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
15package com.android.storagemanager.deletionhelper;
16
17import android.content.Context;
18import android.support.v7.preference.CheckBoxPreference;
19import android.support.v7.preference.Preference;
20import android.support.v7.preference.PreferenceViewHolder;
21import android.text.format.Formatter;
22import android.view.View;
23import android.widget.CheckBox;
24import android.widget.Switch;
25import android.widget.TextView;
26import com.android.storagemanager.deletionhelper.AppStateUsageStatsBridge.UsageStatsState;
27import com.android.storagemanager.R;
28
29import com.android.settingslib.applications.ApplicationsState;
30import com.android.settingslib.applications.ApplicationsState.AppEntry;
31
32/**
33 * Preference item for an app with a switch to signify if it should be uninstalled.
34 * This shows the name and icon of the app along with the days since its last use.
35 */
36public class AppDeletionPreference extends NestedCheckboxPreference {
37    private AppEntry mEntry;
38    private Context mContext;
39
40    public AppDeletionPreference(Context context, AppEntry item) {
41        super(context);
42        mEntry = item;
43        mContext = context;
44        setIcon(item.icon);
45        setTitle(item.label);
46    }
47
48    @Override
49    public void onBindViewHolder(PreferenceViewHolder holder) {
50        super.onBindViewHolder(holder);
51        holder.setDividerAllowedAbove(false);
52    }
53
54    /**
55     * Returns the package name for the app that these preference represents.
56     */
57    public String getPackageName() {
58        synchronized(mEntry) {
59            return mEntry.info.packageName;
60        }
61    }
62
63    public void updateSummary() {
64        if (mEntry.extraInfo == null) return;
65        if (mEntry.size == ApplicationsState.SIZE_UNKNOWN ||
66                mEntry.size == ApplicationsState.SIZE_INVALID) {
67            return;
68        }
69
70        UsageStatsState extraData = (UsageStatsState) mEntry.extraInfo;
71        String fileSize = Formatter.formatFileSize(mContext, mEntry.size);
72        if (extraData.daysSinceLastUse == AppStateUsageStatsBridge.NEVER_USED) {
73            setSummary(mContext.getString(R.string.deletion_helper_app_summary_never_used,
74                    fileSize));
75        } else if (extraData.daysSinceLastUse == AppStateUsageStatsBridge.UNKNOWN_LAST_USE) {
76            setSummary(mContext.getString(R.string.deletion_helper_app_summary_unknown_used,
77                    fileSize));
78        } else {
79            setSummary(mContext.getString(R.string.deletion_helper_app_summary,
80                    fileSize,
81                    extraData.daysSinceLastUse));
82        }
83    }
84
85}
86