blob: 2b6ab5d05a19fbc15e9c3ce764df6e848228c124 [file] [log] [blame]
“Ankitaa6ff8972022-10-03 05:01:00 +00001/*
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
17package com.android.settings.applications;
18
“Ankitad3019d32022-11-12 15:26:15 +000019import static android.content.pm.PackageManager.GET_ACTIVITIES;
20
21import static com.android.settings.Utils.PROPERTY_CLONED_APPS_ENABLED;
“Ankitaa6ff8972022-10-03 05:01:00 +000022
23import android.content.Context;
“Ankitad3019d32022-11-12 15:26:15 +000024import android.os.AsyncTask;
Ankita Vyas6e171e02022-11-23 08:10:17 +000025import android.os.Bundle;
“Ankitad3019d32022-11-12 15:26:15 +000026import android.os.UserHandle;
“Ankitaa6ff8972022-10-03 05:01:00 +000027import android.provider.DeviceConfig;
28
“Ankitad3019d32022-11-12 15:26:15 +000029import androidx.lifecycle.Lifecycle;
30import androidx.lifecycle.LifecycleObserver;
31import androidx.lifecycle.OnLifecycleEvent;
32import androidx.preference.Preference;
33import androidx.preference.PreferenceScreen;
34
35import com.android.settings.R;
36import com.android.settings.Utils;
“Ankitaa6ff8972022-10-03 05:01:00 +000037import com.android.settings.core.BasePreferenceController;
Ankita Vyas6e171e02022-11-23 08:10:17 +000038import com.android.settings.dashboard.profileselector.ProfileSelectFragment;
“Ankitaa6ff8972022-10-03 05:01:00 +000039
“Ankitad3019d32022-11-12 15:26:15 +000040import java.util.Arrays;
41import java.util.List;
42
“Ankitaa6ff8972022-10-03 05:01:00 +000043/**
44 * A preference controller handling the logic for updating the summary of cloned apps.
45 */
“Ankitad3019d32022-11-12 15:26:15 +000046public class ClonedAppsPreferenceController extends BasePreferenceController
47 implements LifecycleObserver {
48 private Preference mPreference;
49 private Context mContext;
“Ankitaa6ff8972022-10-03 05:01:00 +000050
51 public ClonedAppsPreferenceController(Context context, String preferenceKey) {
52 super(context, preferenceKey);
“Ankitad3019d32022-11-12 15:26:15 +000053 mContext = context;
“Ankitaa6ff8972022-10-03 05:01:00 +000054 }
55
56 @Override
“Ankitaa6ff8972022-10-03 05:01:00 +000057 public int getAvailabilityStatus() {
“Ankitad3019d32022-11-12 15:26:15 +000058 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));
“Ankitaa6ff8972022-10-03 05:01:00 +0000116 }
Ankita Vyas6e171e02022-11-23 08:10:17 +0000117
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 }
“Ankitaa6ff8972022-10-03 05:01:00 +0000129}