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.app.settings.SettingsEnums; 19 import android.os.Bundle; 20 import android.view.LayoutInflater; 21 import android.view.Menu; 22 import android.view.MenuInflater; 23 import android.view.MenuItem; 24 import android.view.View; 25 import android.view.ViewGroup; 26 27 import com.android.settings.R; 28 import com.android.settings.SettingsPreferenceFragment; 29 import com.android.settings.development.DeveloperOptionAwareMixin; 30 import com.android.settings.widget.LoadingViewController; 31 32 public class RunningServices extends SettingsPreferenceFragment implements 33 DeveloperOptionAwareMixin { 34 35 private static final int SHOW_RUNNING_SERVICES = 1; 36 private static final int SHOW_BACKGROUND_PROCESSES = 2; 37 38 private RunningProcessesView mRunningProcessesView; 39 private Menu mOptionsMenu; 40 private View mLoadingContainer; 41 private LoadingViewController mLoadingViewController; 42 43 @Override onCreate(Bundle savedInstanceState)44 public void onCreate(Bundle savedInstanceState) { 45 super.onCreate(savedInstanceState); 46 47 getActivity().setTitle(com.android.settingslib.R.string.runningservices_settings_title); 48 } 49 50 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)51 public View onCreateView(LayoutInflater inflater, ViewGroup container, 52 Bundle savedInstanceState) { 53 View rootView = inflater.inflate(R.layout.manage_applications_running, null); 54 mRunningProcessesView = rootView.findViewById(R.id.running_processes); 55 mRunningProcessesView.doCreate(); 56 mLoadingContainer = rootView.findViewById(R.id.loading_container); 57 mLoadingViewController = new LoadingViewController( 58 mLoadingContainer, mRunningProcessesView); 59 60 return rootView; 61 } 62 63 @Override onCreateOptionsMenu(Menu menu, MenuInflater inflater)64 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 65 mOptionsMenu = menu; 66 menu.add(0, SHOW_RUNNING_SERVICES, 1, R.string.show_running_services) 67 .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); 68 menu.add(0, SHOW_BACKGROUND_PROCESSES, 2, R.string.show_background_processes) 69 .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); 70 updateOptionsMenu(); 71 } 72 73 @Override onResume()74 public void onResume() { 75 super.onResume(); 76 boolean haveData = mRunningProcessesView.doResume(this, mRunningProcessesAvail); 77 if (haveData) { 78 mLoadingViewController.showContent(false /* animate */); 79 } else { 80 mLoadingViewController.showLoadingView(); 81 } 82 } 83 84 @Override onPause()85 public void onPause() { 86 super.onPause(); 87 mRunningProcessesView.doPause(); 88 } 89 90 @Override onOptionsItemSelected(MenuItem item)91 public boolean onOptionsItemSelected(MenuItem item) { 92 switch (item.getItemId()) { 93 case SHOW_RUNNING_SERVICES: 94 mRunningProcessesView.mAdapter.setShowBackground(false); 95 break; 96 case SHOW_BACKGROUND_PROCESSES: 97 mRunningProcessesView.mAdapter.setShowBackground(true); 98 break; 99 default: 100 return false; 101 } 102 updateOptionsMenu(); 103 return true; 104 } 105 106 @Override onPrepareOptionsMenu(Menu menu)107 public void onPrepareOptionsMenu(Menu menu) { 108 updateOptionsMenu(); 109 } 110 updateOptionsMenu()111 private void updateOptionsMenu() { 112 boolean showingBackground = mRunningProcessesView.mAdapter.getShowBackground(); 113 mOptionsMenu.findItem(SHOW_RUNNING_SERVICES).setVisible(showingBackground); 114 mOptionsMenu.findItem(SHOW_BACKGROUND_PROCESSES).setVisible(!showingBackground); 115 } 116 117 @Override getMetricsCategory()118 public int getMetricsCategory() { 119 return SettingsEnums.RUNNING_SERVICES; 120 } 121 122 private final Runnable mRunningProcessesAvail = new Runnable() { 123 @Override 124 public void run() { 125 mLoadingViewController.showContent(true /* animate */); 126 } 127 }; 128 129 } 130