Home | History | Annotate | Download | only in applications
      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
     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
     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
     87     public void onResume() {
     88         super.onResume();
     89         mStatsManager.refreshStats(false);
     90         refreshUi();
     91     }
     92 
     93     @Override
     94     public void onDestroy() {
     95         super.onDestroy();
     96         if (getActivity().isChangingConfigurations()) {
     97             mStatsManager.xferStats();
     98         }
     99     }
    100 
    101     @Override
    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>(mFilterSpinner.getContext(),
    107                 R.layout.filter_spinner_item);
    108         mFilterAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    109         for (int i = 0; i < NUM_DURATIONS; i++) {
    110             mFilterAdapter.add(getString(sDurationLabels[i]));
    111         }
    112         mFilterSpinner.setAdapter(mFilterAdapter);
    113         mFilterSpinner.setSelection(mDurationIndex);
    114         mFilterSpinner.setOnItemSelectedListener(this);
    115     }
    116 
    117     @Override
    118     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    119         mDurationIndex = position;
    120         mStatsManager.setDuration(sDurations[position]);
    121         refreshUi();
    122     }
    123 
    124     @Override
    125     public void onNothingSelected(AdapterView<?> parent) {
    126         // Select something.
    127         mFilterSpinner.setSelection(0);
    128     }
    129 
    130     public abstract void refreshUi();
    131 
    132     public static void launchMemoryDetail(SettingsActivity activity, MemInfo memInfo,
    133             ProcStatsPackageEntry entry, boolean includeAppInfo) {
    134         Bundle args = new Bundle();
    135         args.putParcelable(ProcessStatsDetail.EXTRA_PACKAGE_ENTRY, entry);
    136         args.putDouble(ProcessStatsDetail.EXTRA_WEIGHT_TO_RAM, memInfo.weightToRam);
    137         args.putLong(ProcessStatsDetail.EXTRA_TOTAL_TIME, memInfo.memTotalTime);
    138         args.putDouble(ProcessStatsDetail.EXTRA_MAX_MEMORY_USAGE,
    139                 memInfo.usedWeight * memInfo.weightToRam);
    140         args.putDouble(ProcessStatsDetail.EXTRA_TOTAL_SCALE, memInfo.totalScale);
    141         args.putBoolean(AppHeader.EXTRA_HIDE_INFO_BUTTON, !includeAppInfo);
    142         activity.startPreferencePanel(null, ProcessStatsDetail.class.getName(), args,
    143                 R.string.memory_usage, null, null, 0);
    144     }
    145 }
    146