1 /*
2  * Copyright (C) 2015 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.ui.sidepanel;
18 
19 import android.app.ApplicationErrorReport;
20 import android.content.Intent;
21 import android.media.tv.TvInputInfo;
22 import android.view.View;
23 import android.widget.Toast;
24 import com.android.tv.MainActivity;
25 import com.android.tv.R;
26 import com.android.tv.TvApplication;
27 import com.android.tv.TvSingletons;
28 import com.android.tv.common.CommonPreferences;
29 import com.android.tv.common.customization.CustomizationManager;
30 import com.android.tv.common.util.PermissionUtils;
31 import com.android.tv.dialog.PinDialogFragment;
32 import com.android.tv.license.LicenseSideFragment;
33 import com.android.tv.license.Licenses;
34 import com.android.tv.util.Utils;
35 import java.util.ArrayList;
36 import java.util.List;
37 
38 /** Shows TV app settings. */
39 public class SettingsFragment extends SideFragment {
40     private static final String TRACKER_LABEL = "settings";
41 
42     @Override
getTitle()43     protected String getTitle() {
44         return getResources().getString(R.string.side_panel_title_settings);
45     }
46 
47     @Override
getTrackerLabel()48     public String getTrackerLabel() {
49         return TRACKER_LABEL;
50     }
51 
52     @Override
getItemList()53     protected List<Item> getItemList() {
54         List<Item> items = new ArrayList<>();
55         final Item customizeChannelListItem =
56                 new SubMenuItem(
57                         getString(R.string.settings_channel_source_item_customize_channels),
58                         getString(
59                                 R.string
60                                         .settings_channel_source_item_customize_channels_description),
61                         getMainActivity().getOverlayManager().getSideFragmentManager()) {
62                     @Override
63                     protected SideFragment getFragment() {
64                         return new CustomizeChannelListFragment();
65                     }
66 
67                     @Override
68                     protected void onBind(View view) {
69                         super.onBind(view);
70                         setEnabled(false);
71                     }
72 
73                     @Override
74                     protected void onUpdate() {
75                         super.onUpdate();
76                         setEnabled(getChannelDataManager().getChannelCount() != 0);
77                     }
78                 };
79         customizeChannelListItem.setEnabled(false);
80         items.add(customizeChannelListItem);
81         final MainActivity activity = getMainActivity();
82         TvSingletons singletons = TvSingletons.getSingletons(getContext());
83         boolean hasNewInput =
84                 singletons.getSetupUtils().hasNewInput(activity.getTvInputManagerHelper());
85         items.add(
86                 new ActionItem(
87                         getString(R.string.settings_channel_source_item_setup),
88                         hasNewInput
89                                 ? getString(R.string.settings_channel_source_item_setup_new_inputs)
90                                 : null) {
91                     @Override
92                     protected void onSelected() {
93                         closeFragment();
94                         activity.getOverlayManager().showSetupFragment();
95                     }
96                 });
97         if (PermissionUtils.hasModifyParentalControls(getMainActivity())) {
98             items.add(
99                     new ActionItem(
100                             getString(R.string.settings_parental_controls),
101                             getString(
102                                     activity.getParentalControlSettings()
103                                                     .isParentalControlsEnabled()
104                                             ? R.string.option_toggle_parental_controls_on
105                                             : R.string.option_toggle_parental_controls_off)) {
106                         @Override
107                         protected void onSelected() {
108                             getMainActivity()
109                                     .getOverlayManager()
110                                     .getSideFragmentManager()
111                                     .hideSidePanel(true);
112                             PinDialogFragment fragment =
113                                     PinDialogFragment.create(
114                                             PinDialogFragment.PIN_DIALOG_TYPE_ENTER_PIN);
115                             getMainActivity()
116                                     .getOverlayManager()
117                                     .showDialogFragment(
118                                             PinDialogFragment.DIALOG_TAG, fragment, true);
119                         }
120                     });
121         } else {
122             // Note: parental control is turned off, when MODIFY_PARENTAL_CONTROLS is not granted.
123             // But, we may be able to turn on channel lock feature regardless of the permission.
124             // It's TBD.
125         }
126         boolean showTrickplaySetting = false;
127         if (singletons.getBuiltInTunerManager().isPresent()) {
128             for (TvInputInfo inputInfo :
129                     singletons.getTvInputManagerHelper().getTvInputInfos(true, true)) {
130                 if (Utils.isInternalTvInput(getContext(), inputInfo.getId())) {
131                     showTrickplaySetting = true;
132                     break;
133                 }
134             }
135             if (showTrickplaySetting) {
136                 showTrickplaySetting =
137                         CustomizationManager.getTrickplayMode(getContext())
138                                 == CustomizationManager.TRICKPLAY_MODE_ENABLED;
139             }
140         }
141         if (showTrickplaySetting) {
142             items.add(
143                     new SwitchItem(
144                             getString(R.string.settings_trickplay),
145                             getString(R.string.settings_trickplay),
146                             getString(R.string.settings_trickplay_description),
147                             getResources().getInteger(R.integer.trickplay_description_max_lines)) {
148                         @Override
149                         protected void onUpdate() {
150                             super.onUpdate();
151                             boolean enabled =
152                                     CommonPreferences.getTrickplaySetting(getContext())
153                                             != CommonPreferences.TRICKPLAY_SETTING_DISABLED;
154                             setChecked(enabled);
155                         }
156 
157                         @Override
158                         protected void onSelected() {
159                             super.onSelected();
160                             @CommonPreferences.TrickplaySetting
161                             int setting =
162                                     isChecked()
163                                             ? CommonPreferences.TRICKPLAY_SETTING_ENABLED
164                                             : CommonPreferences.TRICKPLAY_SETTING_DISABLED;
165                             CommonPreferences.setTrickplaySetting(getContext(), setting);
166                         }
167                     });
168         }
169         items.add(
170                 new ActionItem(getString(R.string.settings_send_feedback)) {
171                     @Override
172                     protected void onSelected() {
173                         Intent intent = new Intent(Intent.ACTION_APP_ERROR);
174                         ApplicationErrorReport report = new ApplicationErrorReport();
175                         report.packageName = report.processName = getContext().getPackageName();
176                         report.time = System.currentTimeMillis();
177                         report.type = ApplicationErrorReport.TYPE_NONE;
178                         intent.putExtra(Intent.EXTRA_BUG_REPORT, report);
179                         startActivityForResult(intent, 0);
180                     }
181                 });
182         if (Licenses.hasLicenses(getContext())) {
183             items.add(
184                     new SubMenuItem(
185                             getString(R.string.settings_menu_licenses),
186                             getMainActivity().getOverlayManager().getSideFragmentManager()) {
187                         @Override
188                         protected SideFragment getFragment() {
189                             return new LicenseSideFragment();
190                         }
191                     });
192         }
193         // Show version.
194         SimpleActionItem version =
195                 new SimpleActionItem(
196                         getString(R.string.settings_menu_version),
197                         ((TvApplication) activity.getApplicationContext()).getVersionName());
198         version.setClickable(false);
199         items.add(version);
200         return items;
201     }
202 
203     @Override
onResume()204     public void onResume() {
205         super.onResume();
206         if (getChannelDataManager().areAllChannelsHidden()) {
207             Toast.makeText(getActivity(), R.string.msg_all_channels_hidden, Toast.LENGTH_SHORT)
208                     .show();
209         }
210     }
211 }
212