• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.settings.system;
18 
19 import com.android.tv.settings.ActionBehavior;
20 import com.android.tv.settings.ActionKey;
21 import com.android.tv.settings.BaseSettingsActivity;
22 import com.android.tv.settings.R;
23 import com.android.tv.settings.users.RestrictedProfileActivity;
24 import com.android.tv.settings.util.SettingsHelper;
25 import com.android.tv.settings.dialog.old.Action;
26 import com.android.tv.settings.dialog.old.ActionAdapter;
27 
28 import android.content.Context;
29 import android.content.Intent;
30 import android.content.pm.PackageManager;
31 import android.content.pm.ResolveInfo;
32 import android.os.Bundle;
33 import android.os.UserManager;
34 import android.provider.Settings;
35 
36 import java.util.List;
37 
38 /**
39  * Manages app security preferences.
40  * TODO: get a better icon from UX
41  * TODO: implement Notification listener settings
42  */
43 public class SecurityActivity extends BaseSettingsActivity implements ActionAdapter.Listener {
44 
45     private static final String PACKAGE_MIME_TYPE = "application/vnd.android.package-archive";
46     private static final String ACTION_RESTRICTED_PROFILE = "action_restricted_profile";
47 
48     private SettingsHelper mHelper;
49     private boolean mVerifierInstalled;
50 
51     @Override
onCreate(Bundle savedInstanceState)52     protected void onCreate(Bundle savedInstanceState) {
53         mHelper = new SettingsHelper(getApplicationContext());
54         mVerifierInstalled = isVerifierInstalled();
55         // Do this after setting up what's needed.
56         super.onCreate(savedInstanceState);
57     }
58 
59     @Override
getInitialState()60     protected Object getInitialState() {
61         return ActionType.SECURITY_OVERVIEW;
62     }
63 
64     @Override
updateView()65     protected void updateView() {
66         refreshActionList();
67         switch ((ActionType) mState) {
68             case SECURITY_OVERVIEW:
69                 setView(R.string.system_security, R.string.header_category_personal, 0,
70                         R.drawable.ic_settings_security);
71                 break;
72             case SECURITY_UNKNOWN_SOURCES:
73                 setView(R.string.security_unknown_sources_title, R.string.system_security,
74                         R.string.security_unknown_sources_desc, 0);
75                 break;
76             case SECURITY_UNKNOWN_SOURCES_CONFIRM:
77                 setView(R.string.security_unknown_sources_title, R.string.system_security,
78                         R.string.security_unknown_sources_confirm_desc, 0);
79                 break;
80             case SECURITY_VERIFY_APPS:
81                 setView(R.string.security_verify_apps_title, R.string.system_security,
82                         R.string.security_verify_apps_desc, 0);
83                 break;
84             default:
85         }
86     }
87 
88     @Override
refreshActionList()89     protected void refreshActionList() {
90         mActions.clear();
91         boolean isNonMarketAppsAllowed = isNonMarketAppsAllowed();
92         switch ((ActionType) mState) {
93             case SECURITY_OVERVIEW:
94                 mActions.add(ActionType.SECURITY_UNKNOWN_SOURCES.toAction(mResources,
95                         mHelper.getStatusStringFromBoolean(isNonMarketAppsAllowed())));
96                 if (showVerifierSetting()) {
97                     Action verifierAction = ActionType.SECURITY_VERIFY_APPS.toAction(mResources,
98                             mHelper.getStatusStringFromBoolean(isVerifyAppsEnabled()
99                                     && mVerifierInstalled));
100                     verifierAction.setEnabled(mVerifierInstalled);
101                     mActions.add(verifierAction);
102                 }
103                 mActions.add(new Action.Builder().key(ACTION_RESTRICTED_PROFILE)
104                         .title(getString(R.string.launcher_restricted_profile_app_name))
105                         .description(RestrictedProfileActivity.getActionDescription(this))
106                         .intent(new Intent(this, RestrictedProfileActivity.class)).build());
107                 break;
108             case SECURITY_UNKNOWN_SOURCES:
109                 mActions.add(ActionBehavior.ON.toAction(ActionBehavior.getOnKey(
110                         ActionType.SECURITY_UNKNOWN_SOURCES.name()), mResources,
111                         isNonMarketAppsAllowed));
112                 mActions.add(ActionBehavior.OFF.toAction(ActionBehavior.getOffKey(
113                         ActionType.SECURITY_UNKNOWN_SOURCES.name()), mResources,
114                         !isNonMarketAppsAllowed));
115                 break;
116             case SECURITY_UNKNOWN_SOURCES_CONFIRM:
117                 mActions.add(ActionBehavior.OK.toAction(ActionBehavior.getOnKey(
118                         ActionType.SECURITY_UNKNOWN_SOURCES_CONFIRM.name()), mResources));
119                 mActions.add(ActionBehavior.CANCEL.toAction(ActionBehavior.getOffKey(
120                         ActionType.SECURITY_UNKNOWN_SOURCES_CONFIRM.name()), mResources));
121                 break;
122             case SECURITY_VERIFY_APPS:
123                 boolean isVerifyAppsEnabled = mVerifierInstalled && isVerifyAppsEnabled();
124                 mActions.add(ActionBehavior.ON.toAction(ActionBehavior.getOnKey(
125                         ActionType.SECURITY_VERIFY_APPS.name()), mResources,
126                         isVerifyAppsEnabled));
127                 mActions.add(ActionBehavior.OFF.toAction(ActionBehavior.getOffKey(
128                         ActionType.SECURITY_VERIFY_APPS.name()), mResources,
129                         !isVerifyAppsEnabled));
130                 break;
131             default:
132         }
133     }
134 
135     @Override
onActionClicked(Action action)136     public void onActionClicked(Action action) {
137         if (ACTION_RESTRICTED_PROFILE.equals(action.getKey())) {
138             startActivity(action.getIntent());
139             return;
140         }
141         String key = action.getKey();
142         ActionKey<ActionType, ActionBehavior> actionKey = new ActionKey<ActionType, ActionBehavior>(
143                 ActionType.class, ActionBehavior.class, key);
144         final ActionType type = actionKey.getType();
145         final ActionBehavior behavior = actionKey.getBehavior();
146         switch (behavior) {
147             case INIT:
148                 setState(type, true);
149                 break;
150             case ON:
151             case OK:
152                 if (ActionType.SECURITY_UNKNOWN_SOURCES == type) {
153                     setState(ActionType.SECURITY_UNKNOWN_SOURCES_CONFIRM, false);
154                 } else {
155                     setProperty(type, true);
156                     goBack();
157                 }
158                 break;
159             case OFF:
160                 setProperty(type, false);
161                 goBack();
162                 break;
163             case CANCEL:
164                 goBack();
165             default:
166         }
167     }
168 
169     @Override
setProperty(boolean enable)170     protected void setProperty(boolean enable) {
171     }
172 
setProperty(ActionType type, boolean enable)173     private void setProperty(ActionType type, boolean enable) {
174         if (type == ActionType.SECURITY_UNKNOWN_SOURCES_CONFIRM && enable) {
175             setNonMarketAppsAllowed(true);
176         } else if (type == ActionType.SECURITY_UNKNOWN_SOURCES && !enable) {
177             setNonMarketAppsAllowed(false);
178         } else if (type == ActionType.SECURITY_VERIFY_APPS) {
179             setVerifyAppsEnabled(enable);
180         }
181     }
182 
isNonMarketAppsAllowed()183     private boolean isNonMarketAppsAllowed() {
184         return Settings.Global.getInt(getContentResolver(),
185                                       Settings.Global.INSTALL_NON_MARKET_APPS, 0) > 0;
186     }
187 
setNonMarketAppsAllowed(boolean enabled)188     private void setNonMarketAppsAllowed(boolean enabled) {
189         final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
190         if (um.hasUserRestriction(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)) {
191             return;
192         }
193         // Change the system setting
194         Settings.Global.putInt(getContentResolver(), Settings.Global.INSTALL_NON_MARKET_APPS,
195                                 enabled ? 1 : 0);
196     }
197 
isVerifyAppsEnabled()198     private boolean isVerifyAppsEnabled() {
199         return Settings.Global.getInt(getContentResolver(),
200                                       Settings.Global.PACKAGE_VERIFIER_ENABLE, 1) > 0;
201     }
202 
setVerifyAppsEnabled(boolean enable)203     private void setVerifyAppsEnabled(boolean enable) {
204         Settings.Global.putInt(getContentResolver(), Settings.Global.PACKAGE_VERIFIER_ENABLE,
205                 enable ? 1 : 0);
206     }
207 
isVerifierInstalled()208     private boolean isVerifierInstalled() {
209         final PackageManager pm = getPackageManager();
210         final Intent verification = new Intent(Intent.ACTION_PACKAGE_NEEDS_VERIFICATION);
211         verification.setType(PACKAGE_MIME_TYPE);
212         verification.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
213         final List<ResolveInfo> receivers = pm.queryBroadcastReceivers(verification, 0);
214         return (receivers.size() > 0) ? true : false;
215     }
216 
showVerifierSetting()217     private boolean showVerifierSetting() {
218         return Settings.Global.getInt(getContentResolver(),
219                 Settings.Global.PACKAGE_VERIFIER_SETTING_VISIBLE, 1) > 0;
220     }
221 
222 }
223