summaryrefslogtreecommitdiff
path: root/PermissionController/src/com/android/permissioncontroller/permission/ui/UnusedAppsFragment.kt
blob: 48ac04ae5662906309fae042ff44ab22e3998d48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.permissioncontroller.permission.ui

import android.Manifest.permission_group
import android.app.AlertDialog
import android.app.Application
import android.app.Dialog
import android.content.Intent
import android.icu.text.MessageFormat
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.UserHandle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.preference.Preference
import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceScreen
import com.android.permissioncontroller.Constants.EXTRA_SESSION_ID
import com.android.permissioncontroller.Constants.INVALID_SESSION_ID
import com.android.permissioncontroller.R
import com.android.permissioncontroller.hibernation.isHibernationEnabled
import com.android.permissioncontroller.permission.ui.model.UnusedAppsViewModel
import com.android.permissioncontroller.permission.ui.model.UnusedAppsViewModel.UnusedPackageInfo
import com.android.permissioncontroller.permission.ui.model.UnusedAppsViewModel.UnusedPeriod
import com.android.permissioncontroller.permission.ui.model.UnusedAppsViewModel.UnusedPeriod.Companion.allPeriods
import com.android.permissioncontroller.permission.ui.model.UnusedAppsViewModelFactory
import com.android.permissioncontroller.permission.utils.KotlinUtils
import java.text.Collator

/**
 * A fragment displaying all applications that are unused as well as the option to remove them
 * and to open them.
 */
class UnusedAppsFragment<PF, UnusedAppPref> : Fragment()
        where PF : PreferenceFragmentCompat, PF : UnusedAppsFragment.Parent<UnusedAppPref>,
              UnusedAppPref : Preference, UnusedAppPref : RemovablePref {

    private lateinit var viewModel: UnusedAppsViewModel
    private lateinit var collator: Collator
    private var sessionId: Long = 0L
    private var isFirstLoad = false

    companion object {
        const val INFO_MSG_CATEGORY = "info_msg_category"
        private const val SHOW_LOAD_DELAY_MS = 200L
        private const val INFO_MSG_KEY = "info_msg"
        private const val ELEVATION_HIGH = 8f
        private val LOG_TAG = UnusedAppsFragment::class.java.simpleName

        @JvmStatic
        fun <PF, UnusedAppPref> newInstance(): UnusedAppsFragment<PF, UnusedAppPref>
                where PF : PreferenceFragmentCompat, PF : UnusedAppsFragment.Parent<UnusedAppPref>,
                      UnusedAppPref : Preference, UnusedAppPref : RemovablePref {
            return UnusedAppsFragment()
        }

        /**
         * Create the args needed for this fragment
         *
         * @param sessionId The current session Id
         *
         * @return A bundle containing the session Id
         */
        @JvmStatic
        fun createArgs(sessionId: Long): Bundle {
            val bundle = Bundle()
            bundle.putLong(EXTRA_SESSION_ID, sessionId)
            return bundle
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View? {
        val preferenceFragment: PF = requirePreferenceFragment()
        isFirstLoad = true

        collator = Collator.getInstance(
            context!!.getResources().getConfiguration().getLocales().get(0))
        sessionId = arguments!!.getLong(EXTRA_SESSION_ID, INVALID_SESSION_ID)
        val factory = UnusedAppsViewModelFactory(activity!!.application, sessionId)
        viewModel = ViewModelProvider(this, factory).get(UnusedAppsViewModel::class.java)
        viewModel.unusedPackageCategoriesLiveData.observe(this, Observer {
            it?.let { pkgs ->
                updatePackages(pkgs)
                preferenceFragment.setLoadingState(loading = false, animate = true)
            }
        })

        activity?.getActionBar()?.setDisplayHomeAsUpEnabled(true)

        if (!viewModel.unusedPackageCategoriesLiveData.isInitialized) {
            val handler = Handler(Looper.getMainLooper())
            handler.postDelayed({
                if (!viewModel.unusedPackageCategoriesLiveData.isInitialized) {
                    preferenceFragment.setLoadingState(loading = true, animate = true)
                } else {
                    updatePackages(viewModel.unusedPackageCategoriesLiveData.value!!)
                }
            }, SHOW_LOAD_DELAY_MS)
        } else {
            updatePackages(viewModel.unusedPackageCategoriesLiveData.value!!)
        }
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onStart() {
        super.onStart()
        val ab = activity?.actionBar
        if (ab != null) {
            ab!!.setElevation(ELEVATION_HIGH)
        }
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val preferenceFragment: PF = requirePreferenceFragment()
        if (isHibernationEnabled()) {
            preferenceFragment.setTitle(getString(R.string.unused_apps_page_title))
        } else {
            preferenceFragment.setTitle(getString(R.string.permission_removed_page_title))
        }
    }

    private fun requirePreferenceFragment(): PF {
        return requireParentFragment() as PF
    }

    /**
     * Create [PreferenceScreen] in the parent fragment.
     */
    private fun createPreferenceScreen() {
        val preferenceFragment: PF = requirePreferenceFragment()
        val preferenceScreen = preferenceFragment.preferenceManager.inflateFromResource(
            context!!,
            R.xml.unused_app_categories,
            /* rootPreferences= */ null)

        for (period in allPeriods) {
            val periodCat = PreferenceCategory(context!!)
            periodCat.key = period.name
            periodCat.order = 0
            preferenceScreen.addPreference(periodCat)
        }
        preferenceFragment.preferenceScreen = preferenceScreen

        val infoMsgCategory = preferenceScreen.findPreference<PreferenceCategory>(INFO_MSG_CATEGORY)
        val footerPreference = preferenceFragment.createFooterPreference()
        footerPreference.key = INFO_MSG_KEY
        infoMsgCategory?.addPreference(footerPreference)
    }

    private fun updatePackages(categorizedPackages: Map<UnusedPeriod, List<UnusedPackageInfo>>) {
        val preferenceFragment: PF = requirePreferenceFragment()
        if (preferenceFragment.preferenceScreen == null) {
            createPreferenceScreen()
        }
        val preferenceScreen: PreferenceScreen = preferenceFragment.preferenceScreen

        // Remove stale preferences
        val removedPrefs = mutableMapOf<String, UnusedAppPref>()
        for (period in allPeriods) {
            val category = preferenceScreen.findPreference<PreferenceCategory>(period.name)!!
            for (i in 0 until category.preferenceCount) {
                val pref = category.getPreference(i) as UnusedAppPref
                val contains =
                    categorizedPackages[period]?.any { (pkgName, user, _) ->
                        val key = createKey(pkgName, user)
                        pref.key == key
                    }
                if (contains != true) {
                    removedPrefs[pref.key] = pref
                }
            }

            for ((_, pref) in removedPrefs) {
                category.removePreference(pref)
            }
        }

        var allCategoriesEmpty = true
        for ((period, packages) in categorizedPackages) {
            val category = preferenceScreen.findPreference<PreferenceCategory>(period.name)!!
            val months = period.months
            category.title =
                MessageFormat.format(getString(R.string.last_opened_category_title),
                    mapOf("count" to months))
            category.isVisible = packages.isNotEmpty()
            if (packages.isNotEmpty()) {
                allCategoriesEmpty = false
            }

            for ((pkgName, user, isSystemApp, permSet) in packages) {
                val revokedPerms = permSet.toList()
                val key = createKey(pkgName, user)

                var pref = category.findPreference<UnusedAppPref>(key)
                if (pref == null) {
                    pref = removedPrefs[key] ?: preferenceFragment.createUnusedAppPref(
                        activity!!.application, pkgName, user)
                    pref.key = key
                    pref.title = KotlinUtils.getPackageLabel(activity!!.application, pkgName, user)
                }

                pref.setRemoveClickRunnable {
                    viewModel.requestUninstallApp(this, pkgName, user)
                }
                pref.setRemoveComponentEnabled(!isSystemApp)

                pref.onPreferenceClickListener = Preference.OnPreferenceClickListener { _ ->
                    viewModel.navigateToAppInfo(pkgName, user, sessionId)
                    true
                }

                val mostImportant = getMostImportantGroup(revokedPerms)
                val importantLabel = KotlinUtils.getPermGroupLabel(context!!, mostImportant)
                pref.summary = when {
                    revokedPerms.isEmpty() -> null
                    revokedPerms.size == 1 -> getString(R.string.auto_revoked_app_summary_one,
                        importantLabel)
                    revokedPerms.size == 2 -> {
                        val otherLabel = if (revokedPerms[0] == mostImportant) {
                            KotlinUtils.getPermGroupLabel(context!!, revokedPerms[1])
                        } else {
                            KotlinUtils.getPermGroupLabel(context!!, revokedPerms[0])
                        }
                        getString(R.string.auto_revoked_app_summary_two, importantLabel, otherLabel)
                    }
                    else -> getString(R.string.auto_revoked_app_summary_many, importantLabel,
                        "${revokedPerms.size - 1}")
                }
                category.addPreference(pref)
                KotlinUtils.sortPreferenceGroup(category, this::comparePreference, false)
            }
        }

        preferenceFragment.setEmptyState(allCategoriesEmpty)

        if (isFirstLoad) {
            if (categorizedPackages.any { (_, packages) ->
                    packages!!.isNotEmpty()
                }) {
                isFirstLoad = false
            }
            Log.i(LOG_TAG, "sessionId: $sessionId Showed Auto Revoke Page")
            for (period in allPeriods) {
                Log.i(LOG_TAG, "sessionId: $sessionId $period unused: " +
                        "${categorizedPackages[period]}")
                for (revokedPackageInfo in categorizedPackages[period]!!) {
                    for (groupName in revokedPackageInfo.revokedGroups) {
                        val isNewlyRevoked = period.isNewlyUnused()
                        viewModel.logAppView(revokedPackageInfo.packageName,
                            revokedPackageInfo.user, groupName, isNewlyRevoked)
                    }
                }
            }
        }
    }

    private fun comparePreference(lhs: Preference, rhs: Preference): Int {
        var result = collator.compare(lhs.title.toString(),
            rhs.title.toString())
        if (result == 0) {
            result = lhs.key.compareTo(rhs.key)
        }
        return result
    }

    private fun createKey(packageName: String, user: UserHandle): String {
        return "$packageName:${user.identifier}"
    }

    private fun getMostImportantGroup(groupNames: List<String>): String {
        return when {
            groupNames.contains(permission_group.LOCATION) -> permission_group.LOCATION
            groupNames.contains(permission_group.MICROPHONE) -> permission_group.MICROPHONE
            groupNames.contains(permission_group.CAMERA) -> permission_group.CAMERA
            groupNames.contains(permission_group.CONTACTS) -> permission_group.CONTACTS
            groupNames.contains(permission_group.STORAGE) -> permission_group.STORAGE
            groupNames.contains(permission_group.CALENDAR) -> permission_group.CALENDAR
            groupNames.isNotEmpty() -> groupNames[0]
            else -> ""
        }
    }

    private fun createDisableDialog(packageName: String, user: UserHandle) {
        val dialog = DisableDialog()

        val args = Bundle()
        args.putString(Intent.EXTRA_PACKAGE_NAME, packageName)
        args.putParcelable(Intent.EXTRA_USER, user)
        dialog.arguments = args

        dialog.isCancelable = true

        dialog.show(childFragmentManager.beginTransaction(), DisableDialog::class.java.name)
    }

    class DisableDialog : DialogFragment() {
        override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
            val fragment = parentFragment as UnusedAppsFragment<*, *>
            val packageName = arguments!!.getString(Intent.EXTRA_PACKAGE_NAME)!!
            val user = arguments!!.getParcelable<UserHandle>(Intent.EXTRA_USER)!!
            val b = AlertDialog.Builder(context!!)
                .setMessage(R.string.app_disable_dlg_text)
                .setPositiveButton(R.string.app_disable_dlg_positive) { _, _ ->
                    fragment.viewModel.disableApp(packageName, user)
                }
                .setNegativeButton(R.string.cancel, null)
            val d: Dialog = b.create()
            d.setCanceledOnTouchOutside(true)
            return d
        }
    }

    /**
     * Interface that the parent fragment must implement.
     */
    interface Parent<UnusedAppPref> where UnusedAppPref : Preference,
                                          UnusedAppPref : RemovablePref {

        /**
         * Set the title of the current settings page.
         *
         * @param title the title of the current settings page
         */
        fun setTitle(title: CharSequence)

        /**
         * Creates the footer preference that explains why permissions have been re-used and how an
         * app can re-request them.
         */
        fun createFooterPreference(): Preference

        /**
         * Sets the loading state of the view.
         *
         * @param loading whether the view is loading
         * @param animate whether the load state should change with a fade animation
         */
        fun setLoadingState(loading: Boolean, animate: Boolean)

        /**
         * Creates a preference which represents an app that is unused. Has the app icon and label,
         * as well as a button to uninstall/disable the app, and a button to open the app.
         *
         * @param app The current application
         * @param packageName The name of the package whose icon this preference will retrieve
         * @param user The user whose package icon will be retrieved
         */
        fun createUnusedAppPref(
            app: Application,
            packageName: String,
            user: UserHandle,
        ): UnusedAppPref

        /**
         * Updates the state based on whether the content is empty.
         *
         * @param empty whether the content is empty
         */
        fun setEmptyState(empty: Boolean)
    }
}