• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.settings.dashboard.suggestions;
18 
19 import android.app.AutomaticZenRule;
20 import android.app.KeyguardManager;
21 import android.app.NotificationManager;
22 import android.app.WallpaperManager;
23 import android.app.admin.DevicePolicyManager;
24 import android.content.Context;
25 import android.hardware.fingerprint.FingerprintManager;
26 import android.support.annotation.VisibleForTesting;
27 
28 import com.android.ims.ImsManager;
29 import com.android.settings.Settings.FingerprintEnrollSuggestionActivity;
30 import com.android.settings.Settings.FingerprintSuggestionActivity;
31 import com.android.settings.Settings.ScreenLockSuggestionActivity;
32 import com.android.settings.Settings.WifiCallingSuggestionActivity;
33 import com.android.settings.Settings.ZenModeAutomationSuggestionActivity;
34 import com.android.settings.Utils;
35 import com.android.settings.overlay.FeatureFactory;
36 import com.android.settings.wallpaper.WallpaperSuggestionActivity;
37 import com.android.settingslib.drawer.Tile;
38 
39 import java.util.Collection;
40 
41 /**
42  * The Home of all stupidly dynamic Settings Suggestions checks.
43  */
44 public class SuggestionsChecks {
45 
46     private static final String TAG = "SuggestionsChecks";
47     private final Context mContext;
48 
49     private final WallpaperManagerWrapper mWallpaperManager;
50 
SuggestionsChecks(Context context)51     public SuggestionsChecks(Context context) {
52         mContext = context.getApplicationContext();
53         mWallpaperManager = new WallpaperManagerWrapper(mContext);
54     }
55 
isSuggestionComplete(Tile suggestion)56     public boolean isSuggestionComplete(Tile suggestion) {
57         String className = suggestion.intent.getComponent().getClassName();
58         if (className.equals(ZenModeAutomationSuggestionActivity.class.getName())) {
59             return hasEnabledZenAutoRules();
60         } else if (className.equals(WallpaperSuggestionActivity.class.getName())) {
61             return hasWallpaperSet();
62         } else if (className.equals(WifiCallingSuggestionActivity.class.getName())) {
63             return isWifiCallingUnavailableOrEnabled();
64         } else if (className.equals(FingerprintSuggestionActivity.class.getName())) {
65             return isNotSingleFingerprintEnrolled() || !isFingerprintEnabled();
66         } else if (className.equals(ScreenLockSuggestionActivity.class.getName())) {
67             return isDeviceSecured();
68         } else if (className.equals(FingerprintEnrollSuggestionActivity.class.getName())) {
69             FingerprintManager manager = Utils.getFingerprintManagerOrNull(mContext);
70             if (manager == null || !isFingerprintEnabled()) {
71                 return true;
72             }
73             return manager.hasEnrolledFingerprints();
74         }
75 
76         SuggestionFeatureProvider provider =
77                 FeatureFactory.getFactory(mContext).getSuggestionFeatureProvider(mContext);
78         if (provider != null && provider.isPresent(className)) {
79             return provider.isSuggestionCompleted(mContext);
80         }
81 
82         return false;
83     }
84 
isDeviceSecured()85     private boolean isDeviceSecured() {
86         KeyguardManager km = mContext.getSystemService(KeyguardManager.class);
87         return km.isKeyguardSecure();
88     }
89 
isNotSingleFingerprintEnrolled()90     private boolean isNotSingleFingerprintEnrolled() {
91         FingerprintManager manager = Utils.getFingerprintManagerOrNull(mContext);
92         return manager == null || manager.getEnrolledFingerprints().size() != 1;
93     }
94 
isWifiCallingUnavailableOrEnabled()95     public boolean isWifiCallingUnavailableOrEnabled() {
96         if (!ImsManager.isWfcEnabledByPlatform(mContext) ||
97                 !ImsManager.isWfcProvisionedOnDevice(mContext)) {
98             return true;
99         }
100         return ImsManager.isWfcEnabledByUser(mContext)
101                 && ImsManager.isNonTtyOrTtyOnVolteEnabled(mContext);
102     }
103 
hasEnabledZenAutoRules()104     private boolean hasEnabledZenAutoRules() {
105         Collection<AutomaticZenRule> zenRules =
106                 NotificationManager.from(mContext).getAutomaticZenRules().values();
107         for (AutomaticZenRule rule : zenRules) {
108             if (rule.isEnabled()) {
109                 return true;
110             }
111         }
112         return false;
113     }
114 
115     @VisibleForTesting
hasWallpaperSet()116     boolean hasWallpaperSet() {
117         return mWallpaperManager.getWallpaperId(WallpaperManager.FLAG_SYSTEM) > 0;
118     }
119 
isFingerprintEnabled()120     private boolean isFingerprintEnabled() {
121         DevicePolicyManager dpManager =
122                 (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
123         final int dpmFlags = dpManager.getKeyguardDisabledFeatures(null, /* admin */
124                 mContext.getUserId());
125         return (dpmFlags & DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT) == 0;
126     }
127 }
128