• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.settings.dashboard;
16 
17 import android.app.AutomaticZenRule;
18 import android.app.IWallpaperManager;
19 import android.app.IWallpaperManager.Stub;
20 import android.app.IWallpaperManagerCallback;
21 import android.app.KeyguardManager;
22 import android.app.NotificationManager;
23 import android.app.WallpaperManager;
24 import android.app.admin.DevicePolicyManager;
25 import android.content.Context;
26 import android.hardware.fingerprint.FingerprintManager;
27 import android.os.Bundle;
28 import android.os.IBinder;
29 import android.os.RemoteException;
30 import android.os.ServiceManager;
31 
32 import com.android.ims.ImsManager;
33 import com.android.settings.Settings.FingerprintEnrollSuggestionActivity;
34 import com.android.settings.Settings.FingerprintSuggestionActivity;
35 import com.android.settings.Settings.ScreenLockSuggestionActivity;
36 import com.android.settings.Settings.WifiCallingSuggestionActivity;
37 import com.android.settings.Settings.ZenModeAutomationSuggestionActivity;
38 import com.android.settings.WallpaperSuggestionActivity;
39 import com.android.settingslib.drawer.Tile;
40 
41 import java.util.Collection;
42 
43 /**
44  * The Home of all stupidly dynamic Settings Suggestions checks.
45  */
46 public class SuggestionsChecks {
47 
48     private final Context mContext;
49 
SuggestionsChecks(Context context)50     public SuggestionsChecks(Context context) {
51         mContext = context.getApplicationContext();
52     }
53 
isSuggestionComplete(Tile suggestion)54     public boolean isSuggestionComplete(Tile suggestion) {
55         String className = suggestion.intent.getComponent().getClassName();
56         if (className.equals(ZenModeAutomationSuggestionActivity.class.getName())) {
57             return hasEnabledZenAutoRules();
58         } else if (className.equals(WallpaperSuggestionActivity.class.getName())) {
59             return hasWallpaperSet();
60         } else if (className.equals(WifiCallingSuggestionActivity.class.getName())) {
61             return isWifiCallingUnavailableOrEnabled();
62         } else if (className.equals(FingerprintSuggestionActivity.class.getName())) {
63             return isNotSingleFingerprintEnrolled() || !isFingerprintEnabled();
64         } else if (className.equals(ScreenLockSuggestionActivity.class.getName())) {
65             return isDeviceSecured();
66         } else if (className.equals(FingerprintEnrollSuggestionActivity.class.getName())) {
67             return isDeviceSecured() || !isFingerprintEnabled();
68         }
69         return false;
70     }
71 
isDeviceSecured()72     private boolean isDeviceSecured() {
73         KeyguardManager km = mContext.getSystemService(KeyguardManager.class);
74         return km.isKeyguardSecure();
75     }
76 
isNotSingleFingerprintEnrolled()77     private boolean isNotSingleFingerprintEnrolled() {
78         FingerprintManager manager = mContext.getSystemService(FingerprintManager.class);
79         return manager == null || manager.getEnrolledFingerprints().size() != 1;
80     }
81 
isWifiCallingUnavailableOrEnabled()82     public boolean isWifiCallingUnavailableOrEnabled() {
83         if (!ImsManager.isWfcEnabledByPlatform(mContext) ||
84                 !ImsManager.isWfcProvisionedOnDevice(mContext)) {
85             return true;
86         }
87         return ImsManager.isWfcEnabledByUser(mContext)
88                 && ImsManager.isNonTtyOrTtyOnVolteEnabled(mContext);
89     }
90 
hasEnabledZenAutoRules()91     private boolean hasEnabledZenAutoRules() {
92         Collection<AutomaticZenRule> zenRules =
93                 NotificationManager.from(mContext).getAutomaticZenRules().values();
94         for (AutomaticZenRule rule : zenRules) {
95             if (rule.isEnabled()) {
96                 return true;
97             }
98         }
99         return false;
100     }
101 
hasWallpaperSet()102     private boolean hasWallpaperSet() {
103         IBinder b = ServiceManager.getService(Context.WALLPAPER_SERVICE);
104         IWallpaperManager service = Stub.asInterface(b);
105         try {
106             return !service.isSetWallpaperAllowed(mContext.getOpPackageName()) ||
107                     service.getWallpaper(mCallback, WallpaperManager.FLAG_SYSTEM,
108                             new Bundle(), mContext.getUserId()) != null;
109         } catch (RemoteException e) {
110         }
111         return false;
112     }
113 
isFingerprintEnabled()114     private boolean isFingerprintEnabled() {
115         DevicePolicyManager dpManager =
116                 (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
117         final int dpmFlags = dpManager.getKeyguardDisabledFeatures(null, /* admin */
118                 mContext.getUserId());
119         return (dpmFlags & DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT) == 0;
120     }
121 
122     private final IWallpaperManagerCallback mCallback = new IWallpaperManagerCallback.Stub() {
123         @Override
124         public void onWallpaperChanged() throws RemoteException {
125              // Don't care.
126         }
127     };
128 }
129