1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.storagemanager.deletionhelper;
16 
17 import android.content.Context;
18 import androidx.preference.PreferenceViewHolder;
19 import android.text.format.DateUtils;
20 import com.android.storagemanager.R;
21 import com.android.storagemanager.deletionhelper.AppsAsyncLoader.PackageInfo;
22 import java.util.concurrent.TimeUnit;
23 
24 /**
25  * Preference item for an app with a switch to signify if it should be uninstalled. This shows the
26  * name and icon of the app along with the days since its last use.
27  */
28 public class AppDeletionPreference extends NestedDeletionPreference {
29     private PackageInfo mApp;
30     private Context mContext;
31 
AppDeletionPreference(Context context, PackageInfo item)32     public AppDeletionPreference(Context context, PackageInfo item) {
33         super(context);
34         mApp = item;
35         mContext = context;
36         setIcon(item.icon);
37         setTitle(item.label);
38         setItemSize(mApp.size);
39     }
40 
41     @Override
onBindViewHolder(PreferenceViewHolder holder)42     public void onBindViewHolder(PreferenceViewHolder holder) {
43         super.onBindViewHolder(holder);
44         holder.setDividerAllowedAbove(false);
45     }
46 
47     /**
48      * Returns the package name for the app that these preference represents.
49      */
getPackageName()50     public String getPackageName() {
51         return mApp.packageName;
52     }
53 
updateSummary()54     public void updateSummary() {
55         if (mApp == null) return;
56         if (mApp.daysSinceLastUse == AppsAsyncLoader.NEVER_USED) {
57             setSummary(mContext.getString(R.string.deletion_helper_app_summary_never_used));
58         } else if (mApp.daysSinceLastUse == AppsAsyncLoader.UNKNOWN_LAST_USE) {
59             setSummary(mContext.getString(R.string.deletion_helper_app_summary_unknown_used));
60         } else {
61             // Use the formatter for the "yesterday" and "today" cases.
62             if (mApp.daysSinceLastUse <= 1) {
63                 final long now = System.currentTimeMillis();
64                 final long lastUse = now - TimeUnit.DAYS.toMillis(mApp.daysSinceLastUse);
65                 setSummary(
66                         DateUtils.getRelativeTimeSpanString(
67                                 lastUse, now, DateUtils.DAY_IN_MILLIS, 0));
68             } else {
69                 setSummary(
70                         mContext.getString(
71                                 R.string.deletion_helper_app_summary, mApp.daysSinceLastUse));
72             }
73         }
74     }
75 
76 }
77