1/*
2 * Copyright (C) 2016 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.storagemanager.deletionhelper;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.graphics.drawable.Drawable;
22import android.os.Handler;
23import android.support.v7.preference.Preference;
24import android.support.v7.preference.PreferenceViewHolder;
25import android.util.AttributeSet;
26import android.text.format.Formatter;
27
28import android.view.View;
29import com.android.internal.logging.MetricsLogger;
30import com.android.internal.logging.MetricsProto.MetricsEvent;
31import com.android.storagemanager.R;
32
33/**
34 * Preference to handle the deletion of photos and videos in the Deletion Helper.
35 */
36public class PhotosDeletionPreference extends DeletionPreference {
37    public static final int DAYS_TO_KEEP = 30;
38    private boolean mLoaded;
39
40    public PhotosDeletionPreference(Context context, AttributeSet attrs) {
41        super(context, attrs);
42        updatePreferenceText(0, 0);
43        setTitle(R.string.deletion_helper_photos_loading_title);
44        setSummary(R.string.deletion_helper_photos_loading_summary);
45    }
46
47    /**
48     * Updates the title and summary of the preference with fresh information.
49     */
50    public void updatePreferenceText(int items, long bytes) {
51        Context context = getContext();
52        setTitle(context.getString(R.string.deletion_helper_photos_title, items));
53        setSummary(context.getString(R.string.deletion_helper_photos_summary,
54                Formatter.formatFileSize(context, bytes), DAYS_TO_KEEP));
55    }
56
57    @Override
58    public void onFreeableChanged(int items, long bytes) {
59        mLoaded = true;
60        // Because these operations may cause UI churn, we need to ensure they run on the main
61        // thread.
62        new Handler(getContext().getMainLooper()).post(new Runnable() {
63            @Override
64            public void run() {
65                PhotosDeletionPreference.super.onFreeableChanged(items, bytes);
66                updatePreferenceText(items, bytes);
67            }
68        });
69    }
70
71    @Override
72    public boolean onPreferenceChange(Preference preference, Object newValue) {
73        boolean checked = (boolean) newValue;
74        MetricsLogger.action(getContext(), MetricsEvent.ACTION_DELETION_SELECTION_PHOTOS, checked);
75        return super.onPreferenceChange(preference, newValue);
76    }
77
78    @Override
79    public void onBindViewHolder(PreferenceViewHolder holder) {
80        super.onBindViewHolder(holder);
81        holder.findViewById(R.id.progress_bar).setVisibility(mLoaded ? View.GONE : View.VISIBLE);
82
83        holder.findViewById(com.android.internal.R.id.icon)
84                .setVisibility(mLoaded ? View.VISIBLE : View.GONE);
85
86        holder.findViewById(com.android.internal.R.id.checkbox)
87                .setVisibility(mLoaded ? View.VISIBLE : View.GONE);
88    }
89}
90