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 
17 package com.android.settings.deviceinfo.storage;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.graphics.Typeface;
23 import android.os.storage.StorageManager;
24 import android.text.TextPaint;
25 import android.text.style.StyleSpan;
26 import android.util.AttributeSet;
27 import android.view.View;
28 import android.widget.Button;
29 
30 import androidx.preference.Preference;
31 import androidx.preference.PreferenceViewHolder;
32 
33 import com.android.settings.R;
34 import com.android.settings.overlay.FeatureFactory;
35 import com.android.settings.widget.DonutView;
36 
37 /**
38  * StorageSummaryDonutPreference is a preference which summarizes the used and remaining storage left
39  * on a given storage volume. It is visualized with a donut graphing the % used.
40  */
41 public class StorageSummaryDonutPreference extends Preference implements View.OnClickListener {
42     private double mPercent = -1;
43 
StorageSummaryDonutPreference(Context context)44     public StorageSummaryDonutPreference(Context context) {
45         this(context, null);
46     }
47 
StorageSummaryDonutPreference(Context context, AttributeSet attrs)48     public StorageSummaryDonutPreference(Context context, AttributeSet attrs) {
49         super(context, attrs);
50 
51         setLayoutResource(R.layout.storage_summary_donut);
52         setEnabled(false);
53     }
54 
setPercent(long usedBytes, long totalBytes)55     public void setPercent(long usedBytes, long totalBytes) {
56         if (totalBytes == 0) {
57             return;
58         }
59 
60         mPercent = usedBytes / (double) totalBytes;
61     }
62 
63     @Override
onBindViewHolder(PreferenceViewHolder view)64     public void onBindViewHolder(PreferenceViewHolder view) {
65         super.onBindViewHolder(view);
66         view.itemView.setClickable(false);
67 
68         final DonutView donut = (DonutView) view.findViewById(R.id.donut);
69         if (donut != null) {
70             donut.setPercentage(mPercent);
71         }
72 
73         final Button deletionHelperButton = (Button) view.findViewById(R.id.deletion_helper_button);
74         if (deletionHelperButton != null) {
75             deletionHelperButton.setOnClickListener(this);
76         }
77     }
78 
79     @Override
onClick(View v)80     public void onClick(View v) {
81         if (v != null && R.id.deletion_helper_button == v.getId()) {
82             Context context = getContext();
83             FeatureFactory.getFactory(context).getMetricsFeatureProvider().action(
84                     context, SettingsEnums.STORAGE_FREE_UP_SPACE_NOW);
85             Intent intent = new Intent(StorageManager.ACTION_MANAGE_STORAGE);
86             getContext().startActivity(intent);
87         }
88     }
89 
90     private static class BoldLinkSpan extends StyleSpan {
BoldLinkSpan()91         public BoldLinkSpan() {
92             super(Typeface.BOLD);
93         }
94 
95         @Override
updateDrawState(TextPaint ds)96         public void updateDrawState(TextPaint ds) {
97             super.updateDrawState(ds);
98             ds.setColor(ds.linkColor);
99         }
100     }
101 }
102