1 /*
2  * Copyright 2024 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  *      https://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 package com.android.devicediagnostics
17 
18 import android.app.Activity
19 import android.content.Context
20 import android.content.Intent
21 import android.os.Bundle
22 import android.text.format.Formatter
23 import android.util.AttributeSet
24 import androidx.fragment.app.commit
25 import androidx.preference.Preference
26 import androidx.preference.PreferenceFragmentCompat
27 import com.android.devicediagnostics.Protos.StorageInfo
28 import com.android.devicediagnostics.evaluated.getStorageInfo
29 import com.android.settingslib.collapsingtoolbar.CollapsingToolbarBaseActivity
30 import com.android.settingslib.widget.UsageProgressBarPreference
31 import java.text.NumberFormat
32 
33 class StorageInfoFragment(private var diagnostics: StorageInfo) : PreferenceFragmentCompat() {
onCreatePreferencesnull34     override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
35         setPreferencesFromResource(R.xml.storage_preferences, rootKey)
36 
37         findPreference<StorageHealthPreference>("health")!!.also {
38             it.diagnostics = diagnostics
39             if (diagnostics.usefulLifetimeRemaining != -1) {
40                 val locale = getDefaultLocale(activity!!)
41                 val percent = diagnostics.usefulLifetimeRemaining / 100.0f
42                 val percentString = NumberFormat.getPercentInstance(locale).format(percent)
43                 it.setPercent(diagnostics.usefulLifetimeRemaining.toLong(), 100)
44                 it.setBottomSummary(getString(R.string.storage_health_summary_fmt, percentString))
45             } else {
46                 it.isVisible = false
47             }
48         }
49 
50         findPreference<Preference>("total_capacity")!!.also {
51             val desc =
52                 Formatter.formatBytes(
53                     activity!!.resources,
54                     diagnostics.capacityBytes,
55                     Formatter.FLAG_SHORTER
56                 )
57             it.summary =
58                 getString(R.string.storage_total_capacity_summary_fmt, desc.value, desc.units)
59         }
60     }
61 }
62 
63 class StorageHealthPreference(context: Context, attrs: AttributeSet) :
64     UsageProgressBarPreference(context, attrs) {
65 
constructornull66     fun constructor() {}
67 
68     var diagnostics: StorageInfo = StorageInfo.getDefaultInstance()
69 
getSummarynull70     override fun getSummary(): CharSequence? {
71         if (diagnostics.usefulLifetimeRemaining == -1) {
72             return context.getString(R.string.unavailable)
73         }
74         return diagnostics.usefulLifetimeRemaining.toString() + "%"
75     }
76 }
77 
78 class StorageActivity : CollapsingToolbarBaseActivity() {
onCreatenull79     override fun onCreate(savedInstanceState: Bundle?) {
80         super.onCreate(savedInstanceState)
81         setContentView(R.layout.activity_one_fragment)
82         setTitle(R.string.storage_title)
83 
84         if (savedInstanceState == null) {
85             val diagnostics = readStorageInfo()
86             supportFragmentManager.commit {
87                 setReorderingAllowed(true)
88                 add(R.id.fragment_container_view, StorageInfoFragment(diagnostics))
89             }
90         }
91     }
92 
93     // Get either current battery info or battery info from the intent.
readStorageInfonull94     private fun readStorageInfo(): StorageInfo {
95         if (intent.extras != null) {
96             val array = intent.extras!!.getByteArray(INTENT_STORAGE_INFO)
97             return StorageInfo.parseFrom(array)
98         }
99         return getStorageInfo(this)
100     }
101 
102     companion object Static {
103         const val INTENT_STORAGE_INFO = "storage_info"
104 
createIntentnull105         fun createIntent(from: Activity, diagnostics: StorageInfo): Intent {
106             return Intent(from, StorageActivity::class.java)
107                 .putExtra(INTENT_STORAGE_INFO, diagnostics.toByteArray())
108         }
109     }
110 }
111