1 /* 2 * Copyright (C) 2017 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.content.Context; 20 import android.os.storage.VolumeInfo; 21 import android.support.v7.preference.Preference; 22 import android.support.v7.preference.PreferenceScreen; 23 import android.text.TextUtils; 24 import android.text.format.Formatter; 25 26 import com.android.settings.R; 27 import com.android.settings.core.PreferenceControllerMixin; 28 import com.android.settingslib.core.AbstractPreferenceController; 29 import com.android.settingslib.deviceinfo.StorageVolumeProvider; 30 31 /** 32 * StorgaeSummaryPreferenceController updates the donut storage summary preference to have the 33 * correct sizes showing. 34 */ 35 public class StorageSummaryDonutPreferenceController extends AbstractPreferenceController implements 36 PreferenceControllerMixin { 37 private long mUsedBytes; 38 private long mTotalBytes; 39 private StorageSummaryDonutPreference mSummary; 40 StorageSummaryDonutPreferenceController(Context context)41 public StorageSummaryDonutPreferenceController(Context context) { 42 super(context); 43 } 44 45 @Override displayPreference(PreferenceScreen screen)46 public void displayPreference(PreferenceScreen screen) { 47 mSummary = (StorageSummaryDonutPreference) screen.findPreference("pref_summary"); 48 mSummary.setEnabled(true); 49 } 50 51 @Override updateState(Preference preference)52 public void updateState(Preference preference) { 53 super.updateState(preference); 54 StorageSummaryDonutPreference summary = (StorageSummaryDonutPreference) preference; 55 final Formatter.BytesResult result = Formatter.formatBytes(mContext.getResources(), 56 mUsedBytes, 0); 57 summary.setTitle(TextUtils.expandTemplate( 58 mContext.getText(R.string.storage_size_large_alternate), result.value, 59 result.units)); 60 summary.setSummary(mContext.getString(R.string.storage_volume_total, 61 Formatter.formatShortFileSize(mContext, mTotalBytes))); 62 summary.setPercent(mUsedBytes, mTotalBytes); 63 summary.setEnabled(true); 64 } 65 66 /** Invalidates the data on the view and re-renders. */ invalidateData()67 public void invalidateData() { 68 if (mSummary != null) { 69 updateState(mSummary); 70 } 71 } 72 73 @Override isAvailable()74 public boolean isAvailable() { 75 return true; 76 } 77 78 @Override getPreferenceKey()79 public String getPreferenceKey() { 80 return "pref_summary"; 81 } 82 83 /** 84 * Updates the state of the donut preference for the next update. 85 * @param used Total number of used bytes on the summarized volume. 86 * @param total Total number of bytes on the summarized volume. 87 */ updateBytes(long used, long total)88 public void updateBytes(long used, long total) { 89 mUsedBytes = used; 90 mTotalBytes = total; 91 invalidateData(); 92 } 93 94 /** 95 * Updates the state of the donut preference for the next update using volume to summarize. 96 * @param volume VolumeInfo to use to populate the informayion. 97 */ updateSizes(StorageVolumeProvider svp, VolumeInfo volume)98 public void updateSizes(StorageVolumeProvider svp, VolumeInfo volume) { 99 final long sharedDataSize = volume.getPath().getTotalSpace(); 100 long totalSize = svp.getPrimaryStorageSize(); 101 102 if (totalSize <= 0) { 103 totalSize = sharedDataSize; 104 } 105 106 final long usedBytes = totalSize - volume.getPath().getFreeSpace(); 107 updateBytes(usedBytes, totalSize); 108 } 109 } 110