blob: 9d286cf7e6d5e7515fb644c56dda1f8b6161b87a [file] [log] [blame]
Dianne Hackborn83b40f62017-04-26 13:59:47 -07001/*
2 * Copyright (C) 2017 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
17package com.android.systemui;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.content.Context;
22import android.content.DialogInterface;
23import android.content.Intent;
24import android.content.pm.ApplicationInfo;
25import android.content.pm.PackageManager;
26import android.net.Uri;
27import android.os.Bundle;
28import android.provider.Settings;
Dianne Hackborn157a3f92017-05-01 12:33:31 -070029import android.util.IconDrawableFactory;
Dianne Hackborn83b40f62017-04-26 13:59:47 -070030import android.util.Log;
31import android.view.LayoutInflater;
32import android.view.View;
33import android.view.ViewGroup;
34import android.widget.AdapterView;
35import android.widget.ArrayAdapter;
36import android.widget.ImageView;
37import android.widget.ListView;
38import android.widget.TextView;
39
40import com.android.internal.app.AlertActivity;
41import com.android.internal.app.AlertController;
42import com.android.internal.logging.MetricsLogger;
43import com.android.internal.logging.nano.MetricsProto;
44
45import com.android.systemui.R;
46
47import java.util.ArrayList;
48
49/**
50 * Show a list of currently running foreground services (supplied by the caller)
51 * that the user can tap through to their application details.
52 */
53public final class ForegroundServicesDialog extends AlertActivity implements
54 AdapterView.OnItemSelectedListener, DialogInterface.OnClickListener,
55 AlertController.AlertParams.OnPrepareListViewListener {
56
57 private static final String TAG = "ForegroundServicesDialog";
58
59 LayoutInflater mInflater;
60
61 private MetricsLogger mMetricsLogger;
62
63 private String[] mPackages;
64 private PackageItemAdapter mAdapter;
65
66 private DialogInterface.OnClickListener mAppClickListener =
67 new DialogInterface.OnClickListener() {
68 public void onClick(DialogInterface dialog, int which) {
Dianne Hackborn4ae51e02017-05-04 16:53:11 -070069 String pkg = mAdapter.getItem(which).packageName;
Dianne Hackborn83b40f62017-04-26 13:59:47 -070070 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
71 intent.setData(Uri.fromParts("package", pkg, null));
72 startActivity(intent);
73 finish();
74 }
75 };
76
77 @Override
78 protected void onCreate(Bundle savedInstanceState) {
79 super.onCreate(savedInstanceState);
80 Dependency.initDependencies(getApplicationContext());
81
82 mMetricsLogger = Dependency.get(MetricsLogger.class);
83
84 mInflater = LayoutInflater.from(this);
85
86 mAdapter = new PackageItemAdapter(this);
87
88 final AlertController.AlertParams p = mAlertParams;
89 p.mAdapter = mAdapter;
90 p.mOnClickListener = mAppClickListener;
91 p.mCustomTitleView = mInflater.inflate(R.layout.foreground_service_title, null);
92 p.mIsSingleChoice = true;
93 p.mOnItemSelectedListener = this;
94 p.mPositiveButtonText = getString(com.android.internal.R.string.done_label);
95 p.mPositiveButtonListener = this;
96 p.mOnPrepareListViewListener = this;
97
98 updateApps(getIntent());
99 if (mPackages == null) {
100 Log.w(TAG, "No packages supplied");
101 finish();
102 return;
103 }
104
105 setupAlert();
106 }
107
108 @Override
109 protected void onResume() {
110 super.onResume();
111 mMetricsLogger.visible(MetricsProto.MetricsEvent.RUNNING_BACKGROUND_APPS_DIALOG);
112 }
113
114 @Override
115 protected void onPause() {
116 super.onPause();
117 mMetricsLogger.hidden(MetricsProto.MetricsEvent.RUNNING_BACKGROUND_APPS_DIALOG);
118 }
119
120 @Override
121 protected void onNewIntent(Intent intent) {
122 super.onNewIntent(intent);
123 updateApps(intent);
124 }
125
126 @Override
127 protected void onStop() {
128 super.onStop();
129
130 // This is a transient dialog, if the user leaves it then it goes away,
131 // they can return back to it from the notification.
132 if (!isChangingConfigurations()) {
133 finish();
134 }
135 }
136
137 void updateApps(Intent intent) {
138 mPackages = intent.getStringArrayExtra("packages");
139 if (mPackages != null) {
140 mAdapter.setPackages(mPackages);
141 }
142 }
143
144 public void onPrepareListView(ListView listView) {
145 }
146
147 /*
148 * On click of Ok/Cancel buttons
149 */
150 public void onClick(DialogInterface dialog, int which) {
151 finish();
152 }
153
154 public void onItemSelected(AdapterView parent, View view, int position, long id) {
155 }
156
157 public void onNothingSelected(AdapterView parent) {
158 }
159
160 static private class PackageItemAdapter extends ArrayAdapter<ApplicationInfo> {
161 final PackageManager mPm;
162 final LayoutInflater mInflater;
Dianne Hackborn157a3f92017-05-01 12:33:31 -0700163 final IconDrawableFactory mIconDrawableFactory;
Dianne Hackborn83b40f62017-04-26 13:59:47 -0700164
165 public PackageItemAdapter(Context context) {
166 super(context, R.layout.foreground_service_item);
167 mPm = context.getPackageManager();
168 mInflater = LayoutInflater.from(context);
Dianne Hackborn157a3f92017-05-01 12:33:31 -0700169 mIconDrawableFactory = IconDrawableFactory.newInstance(context, true);
Dianne Hackborn83b40f62017-04-26 13:59:47 -0700170 }
171
172 public void setPackages(String[] packages) {
173 clear();
174
175 ArrayList<ApplicationInfo> apps = new ArrayList<>();
176 for (int i = 0; i < packages.length; i++) {
177 try {
178 apps.add(mPm.getApplicationInfo(packages[i],
179 PackageManager.MATCH_KNOWN_PACKAGES));
180 } catch (PackageManager.NameNotFoundException e) {
181 }
182 }
183
184 apps.sort(new ApplicationInfo.DisplayNameComparator(mPm));
185 addAll(apps);
186 }
187
188 public @NonNull
189 View getView(int position, @Nullable View convertView,
190 @NonNull ViewGroup parent) {
191 final View view;
192 if (convertView == null) {
193 view = mInflater.inflate(R.layout.foreground_service_item, parent, false);
194 } else {
195 view = convertView;
196 }
197
198 ImageView icon = view.findViewById(R.id.app_icon);
Dianne Hackborn157a3f92017-05-01 12:33:31 -0700199 icon.setImageDrawable(mIconDrawableFactory.getBadgedIcon(getItem(position)));
Dianne Hackborn83b40f62017-04-26 13:59:47 -0700200
201 TextView label = view.findViewById(R.id.app_name);
202 label.setText(getItem(position).loadLabel(mPm));
203
204 return view;
205 }
206 }
207}