1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.tv.dvr.ui.playback;
18 
19 import android.content.ContentUris;
20 import android.content.Intent;
21 import android.content.res.Configuration;
22 import android.net.Uri;
23 import android.os.Bundle;
24 import android.util.Log;
25 import com.android.tv.R;
26 import com.android.tv.Starter;
27 import com.android.tv.dialog.PinDialogFragment.OnPinCheckedListener;
28 import com.android.tv.dvr.data.RecordedProgram;
29 import com.android.tv.util.Utils;
30 import dagger.android.AndroidInjection;
31 import dagger.android.ContributesAndroidInjector;
32 import dagger.android.DaggerActivity;
33 
34 /** Activity to play a {@link RecordedProgram}. */
35 public class DvrPlaybackActivity extends DaggerActivity implements OnPinCheckedListener {
36     private static final String TAG = "DvrPlaybackActivity";
37     private static final boolean DEBUG = false;
38 
39     private DvrPlaybackOverlayFragment mOverlayFragment;
40     private OnPinCheckedListener mOnPinCheckedListener;
41 
42     @Override
onCreate(Bundle savedInstanceState)43     public void onCreate(Bundle savedInstanceState) {
44         AndroidInjection.inject(this);
45         Starter.start(this);
46         if (DEBUG) Log.d(TAG, "onCreate");
47         super.onCreate(savedInstanceState);
48         setIntent(createProgramIntent(getIntent()));
49         setContentView(R.layout.activity_dvr_playback);
50         mOverlayFragment =
51                 (DvrPlaybackOverlayFragment)
52                         getFragmentManager().findFragmentById(R.id.dvr_playback_controls_fragment);
53     }
54 
55     @Override
onVisibleBehindCanceled()56     public void onVisibleBehindCanceled() {
57         if (DEBUG) Log.d(TAG, "onVisibleBehindCanceled");
58         super.onVisibleBehindCanceled();
59         finish();
60     }
61 
62     @Override
onNewIntent(Intent intent)63     protected void onNewIntent(Intent intent) {
64         setIntent(createProgramIntent(intent));
65         mOverlayFragment.onNewIntent(createProgramIntent(intent));
66     }
67 
68     @Override
onConfigurationChanged(Configuration newConfig)69     public void onConfigurationChanged(Configuration newConfig) {
70         super.onConfigurationChanged(newConfig);
71         float density = getResources().getDisplayMetrics().density;
72         mOverlayFragment.onWindowSizeChanged(
73                 (int) (newConfig.screenWidthDp * density),
74                 (int) (newConfig.screenHeightDp * density));
75     }
76 
createProgramIntent(Intent intent)77     private Intent createProgramIntent(Intent intent) {
78         if (Intent.ACTION_VIEW.equals(intent.getAction())) {
79             Uri uri = intent.getData();
80             if (uri != null) {
81                 long recordedProgramId = ContentUris.parseId(uri);
82                 intent.putExtra(Utils.EXTRA_KEY_RECORDED_PROGRAM_ID, recordedProgramId);
83             }
84         }
85         return intent;
86     }
87 
88     @Override
onPinChecked(boolean checked, int type, String rating)89     public void onPinChecked(boolean checked, int type, String rating) {
90         if (mOnPinCheckedListener != null) {
91             mOnPinCheckedListener.onPinChecked(checked, type, rating);
92         }
93     }
94 
setOnPinCheckListener(OnPinCheckedListener listener)95     void setOnPinCheckListener(OnPinCheckedListener listener) {
96         mOnPinCheckedListener = listener;
97     }
98 
99     /**
100      * Exports {@link DvrPlaybackActivity} for Dagger codegen to create the appropriate injector.
101      */
102     @dagger.Module
103     public abstract static class Module {
104         @ContributesAndroidInjector
contributesDvrPlaybackActivity()105         abstract DvrPlaybackActivity contributesDvrPlaybackActivity();
106 
107         @ContributesAndroidInjector
contributesDvrPlaybackOverlayFragment()108         abstract DvrPlaybackOverlayFragment contributesDvrPlaybackOverlayFragment();
109     }
110 }
111