1 /*
2  * Copyright (C) 2018 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.backup;
18 
19 import android.app.backup.IBackupManager;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ResolveInfo;
24 import android.os.RemoteException;
25 import android.os.ServiceManager;
26 import android.os.UserHandle;
27 import android.os.UserManager;
28 import android.util.Log;
29 
30 import java.util.List;
31 import java.util.Set;
32 import java.util.TreeSet;
33 
34 public class PrivacySettingsUtils {
35     private static final String TAG = "PrivacySettingsUtils";
36     private static final String GSETTINGS_PROVIDER = "com.google.settings";
37 
38     static final String BACKUP_DATA = "backup_data";
39     static final String AUTO_RESTORE = "auto_restore";
40     static final String CONFIGURE_ACCOUNT = "configure_account";
41     static final String BACKUP_INACTIVE = "backup_inactive";
42 
43     // Don't allow any access if this is not an admin user.
44     // TODO: backup/restore currently only works with owner user b/22760572
isAdminUser(final Context context)45     static boolean isAdminUser(final Context context) {
46         return UserManager.get(context).isAdminUser();
47     }
48 
49     /**
50      * Send a {@param key} to check its preference will display in PrivacySettings or not.
51      */
isInvisibleKey(final Context context, final String key)52     static boolean isInvisibleKey(final Context context, final String key) {
53         final Set<String> keysToRemove = getInvisibleKey(context);
54         if (Log.isLoggable(TAG, Log.DEBUG)) {
55             Log.d(TAG,
56                     "keysToRemove size=" + keysToRemove.size() + " keysToRemove=" + keysToRemove);
57         }
58         if (keysToRemove.contains(key)) {
59             return true;
60         }
61         return false;
62     }
63 
getInvisibleKey(final Context context)64     private static Set<String> getInvisibleKey(final Context context) {
65         final IBackupManager backupManager = IBackupManager.Stub.asInterface(
66                 ServiceManager.getService(Context.BACKUP_SERVICE));
67         boolean isServiceActive = false;
68         try {
69             isServiceActive = backupManager.isBackupServiceActive(UserHandle.myUserId());
70         } catch (RemoteException e) {
71             Log.w(TAG, "Failed querying backup manager service activity status. " +
72                     "Assuming it is inactive.");
73         }
74         boolean vendorSpecific = context.getPackageManager().
75                 resolveContentProvider(GSETTINGS_PROVIDER, 0) == null;
76         final Set<String> inVisibleKeys = new TreeSet<>();
77         if (vendorSpecific || isServiceActive) {
78             inVisibleKeys.add(BACKUP_INACTIVE);
79         }
80         if (vendorSpecific || !isServiceActive) {
81             inVisibleKeys.add(BACKUP_DATA);
82             inVisibleKeys.add(AUTO_RESTORE);
83             inVisibleKeys.add(CONFIGURE_ACCOUNT);
84         }
85         return inVisibleKeys;
86     }
87 
updatePrivacyBuffer(final Context context, PrivacySettingsConfigData data)88     public static void updatePrivacyBuffer(final Context context, PrivacySettingsConfigData data) {
89         final IBackupManager backupManager = IBackupManager.Stub.asInterface(
90                 ServiceManager.getService(Context.BACKUP_SERVICE));
91 
92         try {
93             data.setBackupEnabled(backupManager.isBackupEnabled());
94             String transport = backupManager.getCurrentTransport();
95             data.setConfigIntent(validatedActivityIntent(context,
96                     backupManager.getConfigurationIntent(transport), "config"));
97             data.setConfigSummary(backupManager.getDestinationString(transport));
98             data.setManageIntent(validatedActivityIntent(context,
99                     backupManager.getDataManagementIntent(transport), "management"));
100             data.setManageLabel(
101                     backupManager.getDataManagementLabelForUser(UserHandle.myUserId(), transport));
102             data.setBackupGray(false);
103         } catch (RemoteException e) {
104             // leave it 'false' and disable the UI; there's no backup manager
105             //  mBackup.setEnabled(false);
106             data.setBackupGray(true);
107         }
108     }
109 
validatedActivityIntent(final Context context, Intent intent, String logLabel)110     private static Intent validatedActivityIntent(final Context context, Intent intent,
111             String logLabel) {
112         if (intent != null) {
113             PackageManager pm = context.getPackageManager();
114             List<ResolveInfo> resolved = pm.queryIntentActivities(intent, 0);
115             if (resolved == null || resolved.isEmpty()) {
116                 intent = null;
117                 Log.e(TAG, "Backup " + logLabel + " intent " + intent
118                         + " fails to resolve; ignoring");
119             }
120         }
121         return intent;
122     }
123 }