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