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.view.View;
20 import android.widget.Toast;
21 
22 import com.android.tv.MainActivity;
23 import com.android.tv.R;
24 import com.android.tv.TvApplication;
25 import com.android.tv.dialog.PinDialogFragment;
26 import com.android.tv.dialog.WebDialogFragment;
27 import com.android.tv.license.LicenseUtils;
28 import com.android.tv.ui.sidepanel.parentalcontrols.ParentalControlsFragment;
29 import com.android.tv.util.PermissionUtils;
30 import com.android.tv.util.SetupUtils;
31 
32 import java.util.ArrayList;
33 import java.util.List;
34 
35 /**
36  * Shows Live TV settings.
37  */
38 public class SettingsFragment extends SideFragment {
39     private static final String TRACKER_LABEL = "settings";
40 
41     private final long mCurrentChannelId;
42 
SettingsFragment(long currentChannelId)43     public SettingsFragment(long currentChannelId) {
44         mCurrentChannelId = currentChannelId;
45     }
46 
47     /**
48      * Opens a dialog showing open source licenses.
49      */
50     public static final class LicenseActionItem extends ActionItem {
51         public final static String DIALOG_TAG = LicenseActionItem.class.getSimpleName();
52         public static final String TRACKER_LABEL = "Open Source Licenses";
53         private final MainActivity mMainActivity;
54 
LicenseActionItem(MainActivity mainActivity)55         public LicenseActionItem(MainActivity mainActivity) {
56             super(mainActivity.getString(R.string.settings_menu_licenses));
57             mMainActivity = mainActivity;
58         }
59 
60         @Override
onSelected()61         protected void onSelected() {
62             WebDialogFragment dialog = WebDialogFragment.newInstance(LicenseUtils.LICENSE_FILE,
63                     mMainActivity.getString(R.string.dialog_title_licenses), TRACKER_LABEL);
64             mMainActivity.getOverlayManager().showDialogFragment(DIALOG_TAG, dialog, false);
65         }
66     }
67 
68    @Override
getTitle()69     protected String getTitle() {
70         return getResources().getString(R.string.side_panel_title_settings);
71     }
72 
73     @Override
getTrackerLabel()74     public String getTrackerLabel() {
75         return TRACKER_LABEL;
76     }
77 
78     @Override
getItemList()79     protected List<Item> getItemList() {
80         List<Item> items = new ArrayList<>();
81         final Item customizeChannelListItem = new SubMenuItem(
82                 getString(R.string.settings_channel_source_item_customize_channels),
83                 getString(R.string.settings_channel_source_item_customize_channels_description),
84                 0, getMainActivity().getOverlayManager().getSideFragmentManager()) {
85             @Override
86             protected SideFragment getFragment() {
87                 return new CustomizeChannelListFragment(mCurrentChannelId);
88             }
89 
90             @Override
91             protected void onBind(View view) {
92                 super.onBind(view);
93                 setEnabled(false);
94             }
95 
96             @Override
97             protected void onUpdate() {
98                 super.onUpdate();
99                 setEnabled(getChannelDataManager().getChannelCount() != 0);
100             }
101         };
102         customizeChannelListItem.setEnabled(false);
103         items.add(customizeChannelListItem);
104         final MainActivity activity = getMainActivity();
105         boolean hasNewInput = SetupUtils.getInstance(activity).hasNewInput(
106                 activity.getTvInputManagerHelper());
107         items.add(new ActionItem(
108                 getString(R.string.settings_channel_source_item_setup),
109                 hasNewInput ? getString(R.string.settings_channel_source_item_setup_new_inputs)
110                         : null) {
111             @Override
112             protected void onSelected() {
113                 closeFragment();
114                 activity.getOverlayManager().showSetupFragment();
115             }
116         });
117         if (PermissionUtils.hasModifyParentalControls(getMainActivity())) {
118             items.add(new ActionItem(getString(R.string.settings_parental_controls),
119                     getString(activity.getParentalControlSettings().isParentalControlsEnabled()
120                             ? R.string.option_toggle_parental_controls_on
121                             : R.string.option_toggle_parental_controls_off)) {
122                 @Override
123                 protected void onSelected() {
124                     final MainActivity tvActivity = getMainActivity();
125                     final SideFragmentManager sideFragmentManager = tvActivity.getOverlayManager()
126                             .getSideFragmentManager();
127                     sideFragmentManager.hideSidePanel(true);
128                     PinDialogFragment fragment = new PinDialogFragment(
129                             PinDialogFragment.PIN_DIALOG_TYPE_ENTER_PIN,
130                             new PinDialogFragment.ResultListener() {
131                                 @Override
132                                 public void done(boolean success) {
133                                     if (success) {
134                                         sideFragmentManager.show(new ParentalControlsFragment(),
135                                                 false);
136                                         sideFragmentManager.showSidePanel(true);
137                                     } else {
138                                         sideFragmentManager.hideAll(false);
139                                     }
140                                 }
141                             });
142                     tvActivity.getOverlayManager().showDialogFragment(PinDialogFragment.DIALOG_TAG,
143                             fragment, true);
144                 }
145             });
146         } else {
147             // Note: parental control is turned off, when MODIFY_PARENTAL_CONTROLS is not granted.
148             // But, we may be able to turn on channel lock feature regardless of the permission.
149             // It's TBD.
150         }
151         if (LicenseUtils.hasLicenses(activity.getAssets())) {
152             items.add(new LicenseActionItem(activity));
153         }
154         // Show version.
155         items.add(new SimpleItem(getString(R.string.settings_menu_version),
156                 ((TvApplication) activity.getApplicationContext()).getVersionName()));
157         return items;
158     }
159 
160     @Override
onResume()161     public void onResume() {
162         super.onResume();
163         if (getChannelDataManager().areAllChannelsHidden()) {
164             Toast.makeText(getActivity(), R.string.msg_all_channels_hidden, Toast.LENGTH_SHORT)
165                     .show();
166         }
167     }
168 }
169