“Ankita | a6ff897 | 2022-10-03 05:01:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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.applications; |
| 18 | |
“Ankita | d3019d3 | 2022-11-12 15:26:15 +0000 | [diff] [blame] | 19 | import static android.content.pm.PackageManager.GET_ACTIVITIES; |
| 20 | |
| 21 | import static com.android.settings.Utils.PROPERTY_CLONED_APPS_ENABLED; |
“Ankita | a6ff897 | 2022-10-03 05:01:00 +0000 | [diff] [blame] | 22 | |
| 23 | import android.content.Context; |
“Ankita | d3019d3 | 2022-11-12 15:26:15 +0000 | [diff] [blame] | 24 | import android.os.AsyncTask; |
Ankita Vyas | 6e171e0 | 2022-11-23 08:10:17 +0000 | [diff] [blame] | 25 | import android.os.Bundle; |
“Ankita | d3019d3 | 2022-11-12 15:26:15 +0000 | [diff] [blame] | 26 | import android.os.UserHandle; |
“Ankita | a6ff897 | 2022-10-03 05:01:00 +0000 | [diff] [blame] | 27 | import android.provider.DeviceConfig; |
| 28 | |
“Ankita | d3019d3 | 2022-11-12 15:26:15 +0000 | [diff] [blame] | 29 | import androidx.lifecycle.Lifecycle; |
| 30 | import androidx.lifecycle.LifecycleObserver; |
| 31 | import androidx.lifecycle.OnLifecycleEvent; |
| 32 | import androidx.preference.Preference; |
| 33 | import androidx.preference.PreferenceScreen; |
| 34 | |
| 35 | import com.android.settings.R; |
| 36 | import com.android.settings.Utils; |
“Ankita | a6ff897 | 2022-10-03 05:01:00 +0000 | [diff] [blame] | 37 | import com.android.settings.core.BasePreferenceController; |
Ankita Vyas | 6e171e0 | 2022-11-23 08:10:17 +0000 | [diff] [blame] | 38 | import com.android.settings.dashboard.profileselector.ProfileSelectFragment; |
“Ankita | a6ff897 | 2022-10-03 05:01:00 +0000 | [diff] [blame] | 39 | |
“Ankita | d3019d3 | 2022-11-12 15:26:15 +0000 | [diff] [blame] | 40 | import java.util.Arrays; |
| 41 | import java.util.List; |
| 42 | |
“Ankita | a6ff897 | 2022-10-03 05:01:00 +0000 | [diff] [blame] | 43 | /** |
| 44 | * A preference controller handling the logic for updating the summary of cloned apps. |
| 45 | */ |
“Ankita | d3019d3 | 2022-11-12 15:26:15 +0000 | [diff] [blame] | 46 | public class ClonedAppsPreferenceController extends BasePreferenceController |
| 47 | implements LifecycleObserver { |
| 48 | private Preference mPreference; |
| 49 | private Context mContext; |
“Ankita | a6ff897 | 2022-10-03 05:01:00 +0000 | [diff] [blame] | 50 | |
| 51 | public ClonedAppsPreferenceController(Context context, String preferenceKey) { |
| 52 | super(context, preferenceKey); |
“Ankita | d3019d3 | 2022-11-12 15:26:15 +0000 | [diff] [blame] | 53 | mContext = context; |
“Ankita | a6ff897 | 2022-10-03 05:01:00 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | @Override |
“Ankita | a6ff897 | 2022-10-03 05:01:00 +0000 | [diff] [blame] | 57 | public int getAvailabilityStatus() { |
“Ankita | d3019d3 | 2022-11-12 15:26:15 +0000 | [diff] [blame] | 58 | return DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_APP_CLONING, |
| 59 | PROPERTY_CLONED_APPS_ENABLED, false) ? AVAILABLE : UNSUPPORTED_ON_DEVICE; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void displayPreference(PreferenceScreen screen) { |
| 64 | super.displayPreference(screen); |
| 65 | mPreference = screen.findPreference(getPreferenceKey()); |
| 66 | } |
| 67 | /** |
| 68 | * On lifecycle resume event. |
| 69 | */ |
| 70 | @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) |
| 71 | public void onResume() { |
| 72 | updatePreferenceSummary(); |
| 73 | } |
| 74 | |
| 75 | private void updatePreferenceSummary() { |
| 76 | new AsyncTask<Void, Void, Integer[]>() { |
| 77 | |
| 78 | @Override |
| 79 | protected Integer[] doInBackground(Void... unused) { |
| 80 | // Get list of allowlisted cloneable apps. |
| 81 | List<String> cloneableApps = Arrays.asList( |
| 82 | mContext.getResources().getStringArray( |
| 83 | com.android.internal.R.array.cloneable_apps)); |
| 84 | List<String> primaryUserApps = mContext.getPackageManager() |
| 85 | .getInstalledPackagesAsUser(GET_ACTIVITIES, |
| 86 | UserHandle.myUserId()).stream().map(x -> x.packageName).toList(); |
| 87 | // Count number of installed apps in system user. |
| 88 | int availableAppsCount = (int) cloneableApps.stream() |
| 89 | .filter(x -> primaryUserApps.contains(x)).count(); |
| 90 | |
| 91 | int cloneUserId = Utils.getCloneUserId(mContext); |
| 92 | if (cloneUserId == -1) { |
| 93 | return new Integer[]{0, availableAppsCount}; |
| 94 | } |
| 95 | // Get all apps in clone profile if present. |
| 96 | List<String> cloneProfileApps = mContext.getPackageManager() |
| 97 | .getInstalledPackagesAsUser(GET_ACTIVITIES, |
| 98 | cloneUserId).stream().map(x -> x.packageName).toList(); |
| 99 | // Count number of allowlisted app present in clone profile. |
| 100 | int clonedAppsCount = (int) cloneableApps.stream() |
| 101 | .filter(x -> cloneProfileApps.contains(x)).count(); |
| 102 | |
| 103 | return new Integer[]{clonedAppsCount, availableAppsCount - clonedAppsCount}; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | protected void onPostExecute(Integer[] countInfo) { |
| 108 | updateSummary(countInfo[0], countInfo[1]); |
| 109 | } |
| 110 | }.execute(); |
| 111 | } |
| 112 | |
| 113 | private void updateSummary(int clonedAppsCount, int availableAppsCount) { |
| 114 | mPreference.setSummary(mContext.getResources().getString( |
| 115 | R.string.cloned_apps_summary, clonedAppsCount, availableAppsCount)); |
“Ankita | a6ff897 | 2022-10-03 05:01:00 +0000 | [diff] [blame] | 116 | } |
Ankita Vyas | 6e171e0 | 2022-11-23 08:10:17 +0000 | [diff] [blame] | 117 | |
| 118 | @Override |
| 119 | public boolean handlePreferenceTreeClick(Preference preference) { |
| 120 | // Add this extra so that work tab is not displayed on Cloned Apps page. |
| 121 | if (getPreferenceKey().equals(preference.getKey())) { |
| 122 | final Bundle extras = preference.getExtras(); |
| 123 | extras.putInt(ProfileSelectFragment.EXTRA_PROFILE, |
| 124 | ProfileSelectFragment.ProfileType.PERSONAL); |
| 125 | } |
| 126 | |
| 127 | return super.handlePreferenceTreeClick(preference); |
| 128 | } |
“Ankita | a6ff897 | 2022-10-03 05:01:00 +0000 | [diff] [blame] | 129 | } |