1 /* 2 * Copyright (C) 2018 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.applications.appinfo; 18 19 import android.content.Context; 20 import android.net.NetworkTemplate; 21 import android.os.Bundle; 22 import android.os.Process; 23 import android.text.format.DateUtils; 24 import android.text.format.Formatter; 25 26 import androidx.annotation.VisibleForTesting; 27 import androidx.loader.app.LoaderManager; 28 import androidx.loader.content.Loader; 29 import androidx.preference.Preference; 30 import androidx.preference.PreferenceScreen; 31 32 import com.android.settings.R; 33 import com.android.settings.SettingsPreferenceFragment; 34 import com.android.settings.Utils; 35 import com.android.settings.datausage.AppDataUsage; 36 import com.android.settings.datausage.lib.NetworkTemplates; 37 import com.android.settings.spa.app.appinfo.AppDataUsagePreferenceKt; 38 import com.android.settingslib.AppItem; 39 import com.android.settingslib.applications.AppUtils; 40 import com.android.settingslib.core.lifecycle.LifecycleObserver; 41 import com.android.settingslib.core.lifecycle.events.OnPause; 42 import com.android.settingslib.core.lifecycle.events.OnResume; 43 import com.android.settingslib.net.NetworkCycleDataForUid; 44 import com.android.settingslib.net.NetworkCycleDataForUidLoader; 45 46 import java.util.List; 47 48 /** 49 * @deprecated Will be removed, use {@link AppDataUsagePreferenceKt} instead. 50 */ 51 @Deprecated(forRemoval = true) 52 public class AppDataUsagePreferenceController extends AppInfoPreferenceControllerBase 53 implements LoaderManager.LoaderCallbacks<List<NetworkCycleDataForUid>>, LifecycleObserver, 54 OnResume, OnPause { 55 56 private List<NetworkCycleDataForUid> mAppUsageData; 57 AppDataUsagePreferenceController(Context context, String key)58 public AppDataUsagePreferenceController(Context context, String key) { 59 super(context, key); 60 } 61 62 @Override getAvailabilityStatus()63 public int getAvailabilityStatus() { 64 return isBandwidthControlEnabled() ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; 65 } 66 67 @Override displayPreference(PreferenceScreen screen)68 public void displayPreference(PreferenceScreen screen) { 69 super.displayPreference(screen); 70 mPreference.setEnabled(AppUtils.isAppInstalled(mAppEntry)); 71 } 72 73 @Override updateState(Preference preference)74 public void updateState(Preference preference) { 75 preference.setSummary(getDataSummary()); 76 } 77 78 @Override onResume()79 public void onResume() { 80 if (isAvailable()) { 81 final int uid = mParent.getAppEntry().info.uid; 82 final AppItem app = new AppItem(uid); 83 app.addUid(uid); 84 mParent.getLoaderManager().restartLoader(mParent.LOADER_CHART_DATA, null /* args */, 85 this); 86 } 87 } 88 89 @Override onPause()90 public void onPause() { 91 if (isAvailable()) { 92 mParent.getLoaderManager().destroyLoader(mParent.LOADER_CHART_DATA); 93 } 94 } 95 96 @Override onCreateLoader(int id, Bundle args)97 public Loader<List<NetworkCycleDataForUid>> onCreateLoader(int id, Bundle args) { 98 final NetworkTemplate template = NetworkTemplates.INSTANCE.getDefaultTemplate(mContext); 99 final int uid = mParent.getAppEntry().info.uid; 100 101 final NetworkCycleDataForUidLoader.Builder builder = 102 NetworkCycleDataForUidLoader.builder(mContext); 103 builder.setRetrieveDetail(false) 104 .setNetworkTemplate(template); 105 106 builder.addUid(uid); 107 if (Process.isApplicationUid(uid)) { 108 // Also add in network usage for the app's SDK sandbox 109 builder.addUid(Process.toSdkSandboxUid(uid)); 110 } 111 return builder.build(); 112 } 113 114 @Override onLoadFinished(Loader<List<NetworkCycleDataForUid>> loader, List<NetworkCycleDataForUid> data)115 public void onLoadFinished(Loader<List<NetworkCycleDataForUid>> loader, 116 List<NetworkCycleDataForUid> data) { 117 mAppUsageData = data; 118 updateState(mPreference); 119 } 120 121 @Override onLoaderReset(Loader<List<NetworkCycleDataForUid>> loader)122 public void onLoaderReset(Loader<List<NetworkCycleDataForUid>> loader) { 123 // Leave last result. 124 } 125 126 @Override getDetailFragmentClass()127 protected Class<? extends SettingsPreferenceFragment> getDetailFragmentClass() { 128 return AppDataUsage.class; 129 } 130 getDataSummary()131 private CharSequence getDataSummary() { 132 if (mAppUsageData != null) { 133 long totalBytes = 0; 134 long startTime = System.currentTimeMillis(); 135 for (NetworkCycleDataForUid data : mAppUsageData) { 136 totalBytes += data.getTotalUsage(); 137 final long cycleStart = data.getStartTime(); 138 if (cycleStart < startTime) { 139 startTime = cycleStart; 140 } 141 } 142 if (totalBytes == 0) { 143 return mContext.getString(R.string.no_data_usage); 144 } 145 return mContext.getString(R.string.data_summary_format, 146 Formatter.formatFileSize(mContext, totalBytes, Formatter.FLAG_IEC_UNITS), 147 DateUtils.formatDateTime(mContext, startTime, 148 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_MONTH)); 149 } 150 return mContext.getString(R.string.computing_size); 151 } 152 153 @VisibleForTesting isBandwidthControlEnabled()154 boolean isBandwidthControlEnabled() { 155 return Utils.isBandwidthControlEnabled(); 156 } 157 158 } 159