1 /* 2 * Copyright (C) 2015 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 package com.android.settings.applications; 17 18 import android.os.Bundle; 19 import android.view.View; 20 import android.view.ViewGroup; 21 import android.widget.AdapterView; 22 import android.widget.AdapterView.OnItemSelectedListener; 23 import android.widget.ArrayAdapter; 24 import android.widget.Spinner; 25 26 import com.android.internal.app.procstats.ProcessStats; 27 import com.android.settings.AppHeader; 28 import com.android.settings.R; 29 import com.android.settings.SettingsActivity; 30 import com.android.settings.SettingsPreferenceFragment; 31 import com.android.settings.applications.ProcStatsData.MemInfo; 32 33 public abstract class ProcessStatsBase extends SettingsPreferenceFragment 34 implements OnItemSelectedListener { 35 private static final String DURATION = "duration"; 36 37 protected static final String ARG_TRANSFER_STATS = "transfer_stats"; 38 protected static final String ARG_DURATION_INDEX = "duration_index"; 39 40 protected static final int NUM_DURATIONS = 4; 41 42 // The actual duration value to use for each duration option. Note these 43 // are lower than the actual duration, since our durations are computed in 44 // batches of 3 hours so we want to allow the time we use to be slightly 45 // smaller than the actual time selected instead of bumping up to 3 hours 46 // beyond it. 47 private static final long DURATION_QUANTUM = ProcessStats.COMMIT_PERIOD; 48 protected static long[] sDurations = new long[] { 49 3 * 60 * 60 * 1000 - DURATION_QUANTUM / 2, 6 * 60 *60 * 1000 - DURATION_QUANTUM / 2, 50 12 * 60 * 60 * 1000 - DURATION_QUANTUM / 2, 24 * 60 * 60 * 1000 - DURATION_QUANTUM / 2 51 }; 52 protected static int[] sDurationLabels = new int[] { 53 R.string.menu_duration_3h, R.string.menu_duration_6h, 54 R.string.menu_duration_12h, R.string.menu_duration_1d 55 }; 56 57 private ViewGroup mSpinnerHeader; 58 private Spinner mFilterSpinner; 59 private ArrayAdapter<String> mFilterAdapter; 60 61 protected ProcStatsData mStatsManager; 62 protected int mDurationIndex; 63 64 @Override onCreate(Bundle icicle)65 public void onCreate(Bundle icicle) { 66 super.onCreate(icicle); 67 68 Bundle args = getArguments(); 69 mStatsManager = new ProcStatsData(getActivity(), icicle != null 70 || (args != null && args.getBoolean(ARG_TRANSFER_STATS, false))); 71 72 mDurationIndex = icicle != null 73 ? icicle.getInt(ARG_DURATION_INDEX) 74 : args != null ? args.getInt(ARG_DURATION_INDEX) : 0; 75 mStatsManager.setDuration(icicle != null 76 ? icicle.getLong(DURATION, sDurations[0]) : sDurations[0]); 77 } 78 79 @Override onSaveInstanceState(Bundle outState)80 public void onSaveInstanceState(Bundle outState) { 81 super.onSaveInstanceState(outState); 82 outState.putLong(DURATION, mStatsManager.getDuration()); 83 outState.putInt(ARG_DURATION_INDEX, mDurationIndex); 84 } 85 86 @Override onResume()87 public void onResume() { 88 super.onResume(); 89 mStatsManager.refreshStats(false); 90 refreshUi(); 91 } 92 93 @Override onDestroy()94 public void onDestroy() { 95 super.onDestroy(); 96 if (getActivity().isChangingConfigurations()) { 97 mStatsManager.xferStats(); 98 } 99 } 100 101 @Override onViewCreated(View view, Bundle savedInstanceState)102 public void onViewCreated(View view, Bundle savedInstanceState) { 103 super.onViewCreated(view, savedInstanceState); 104 mSpinnerHeader = (ViewGroup) setPinnedHeaderView(R.layout.apps_filter_spinner); 105 mFilterSpinner = (Spinner) mSpinnerHeader.findViewById(R.id.filter_spinner); 106 mFilterAdapter = new ArrayAdapter<String>(getActivity(), R.layout.filter_spinner_item); 107 mFilterAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 108 for (int i = 0; i < NUM_DURATIONS; i++) { 109 mFilterAdapter.add(getString(sDurationLabels[i])); 110 } 111 mFilterSpinner.setAdapter(mFilterAdapter); 112 mFilterSpinner.setSelection(mDurationIndex); 113 mFilterSpinner.setOnItemSelectedListener(this); 114 } 115 116 @Override onItemSelected(AdapterView<?> parent, View view, int position, long id)117 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 118 mDurationIndex = position; 119 mStatsManager.setDuration(sDurations[position]); 120 refreshUi(); 121 } 122 123 @Override onNothingSelected(AdapterView<?> parent)124 public void onNothingSelected(AdapterView<?> parent) { 125 // Select something. 126 mFilterSpinner.setSelection(0); 127 } 128 refreshUi()129 public abstract void refreshUi(); 130 launchMemoryDetail(SettingsActivity activity, MemInfo memInfo, ProcStatsPackageEntry entry, boolean includeAppInfo)131 public static void launchMemoryDetail(SettingsActivity activity, MemInfo memInfo, 132 ProcStatsPackageEntry entry, boolean includeAppInfo) { 133 Bundle args = new Bundle(); 134 args.putParcelable(ProcessStatsDetail.EXTRA_PACKAGE_ENTRY, entry); 135 args.putDouble(ProcessStatsDetail.EXTRA_WEIGHT_TO_RAM, memInfo.weightToRam); 136 args.putLong(ProcessStatsDetail.EXTRA_TOTAL_TIME, memInfo.memTotalTime); 137 args.putDouble(ProcessStatsDetail.EXTRA_MAX_MEMORY_USAGE, 138 memInfo.usedWeight * memInfo.weightToRam); 139 args.putDouble(ProcessStatsDetail.EXTRA_TOTAL_SCALE, memInfo.totalScale); 140 args.putBoolean(AppHeader.EXTRA_HIDE_INFO_BUTTON, !includeAppInfo); 141 activity.startPreferencePanel(ProcessStatsDetail.class.getName(), args, 142 R.string.memory_usage, null, null, 0); 143 } 144 } 145