blob: 710980a274a225625b62ee715fa2dfc4d9fcb5dc [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
Dianne Hackborn83b40f62017-04-26 13:59:47 -070045import java.util.ArrayList;
46
Dave Mankoffeb593ae2019-09-04 11:31:55 -040047import javax.inject.Inject;
48
Dianne Hackborn83b40f62017-04-26 13:59:47 -070049/**
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
Dave Mankoffeb593ae2019-09-04 11:31:55 -040077 @Inject
78 ForegroundServicesDialog() {
79 super();
80 }
81
Dianne Hackborn83b40f62017-04-26 13:59:47 -070082 @Override
83 protected void onCreate(Bundle savedInstanceState) {
84 super.onCreate(savedInstanceState);
Dianne Hackborn83b40f62017-04-26 13:59:47 -070085
86 mMetricsLogger = Dependency.get(MetricsLogger.class);
87
88 mInflater = LayoutInflater.from(this);
89
90 mAdapter = new PackageItemAdapter(this);
91
92 final AlertController.AlertParams p = mAlertParams;
93 p.mAdapter = mAdapter;
94 p.mOnClickListener = mAppClickListener;
95 p.mCustomTitleView = mInflater.inflate(R.layout.foreground_service_title, null);
96 p.mIsSingleChoice = true;
97 p.mOnItemSelectedListener = this;
98 p.mPositiveButtonText = getString(com.android.internal.R.string.done_label);
99 p.mPositiveButtonListener = this;
100 p.mOnPrepareListViewListener = this;
101
102 updateApps(getIntent());
103 if (mPackages == null) {
104 Log.w(TAG, "No packages supplied");
105 finish();
106 return;
107 }
108
109 setupAlert();
110 }
111
112 @Override
113 protected void onResume() {
114 super.onResume();
115 mMetricsLogger.visible(MetricsProto.MetricsEvent.RUNNING_BACKGROUND_APPS_DIALOG);
116 }
117
118 @Override
119 protected void onPause() {
120 super.onPause();
121 mMetricsLogger.hidden(MetricsProto.MetricsEvent.RUNNING_BACKGROUND_APPS_DIALOG);
122 }
123
124 @Override
125 protected void onNewIntent(Intent intent) {
126 super.onNewIntent(intent);
127 updateApps(intent);
128 }
129
130 @Override
131 protected void onStop() {
132 super.onStop();
133
134 // This is a transient dialog, if the user leaves it then it goes away,
135 // they can return back to it from the notification.
136 if (!isChangingConfigurations()) {
137 finish();
138 }
139 }
140
141 void updateApps(Intent intent) {
142 mPackages = intent.getStringArrayExtra("packages");
143 if (mPackages != null) {
144 mAdapter.setPackages(mPackages);
145 }
146 }
147
148 public void onPrepareListView(ListView listView) {
149 }
150
151 /*
152 * On click of Ok/Cancel buttons
153 */
154 public void onClick(DialogInterface dialog, int which) {
155 finish();
156 }
157
158 public void onItemSelected(AdapterView parent, View view, int position, long id) {
159 }
160
161 public void onNothingSelected(AdapterView parent) {
162 }
163
164 static private class PackageItemAdapter extends ArrayAdapter<ApplicationInfo> {
165 final PackageManager mPm;
166 final LayoutInflater mInflater;
Dianne Hackborn157a3f92017-05-01 12:33:31 -0700167 final IconDrawableFactory mIconDrawableFactory;
Dianne Hackborn83b40f62017-04-26 13:59:47 -0700168
169 public PackageItemAdapter(Context context) {
170 super(context, R.layout.foreground_service_item);
171 mPm = context.getPackageManager();
172 mInflater = LayoutInflater.from(context);
Dianne Hackborn157a3f92017-05-01 12:33:31 -0700173 mIconDrawableFactory = IconDrawableFactory.newInstance(context, true);
Dianne Hackborn83b40f62017-04-26 13:59:47 -0700174 }
175
176 public void setPackages(String[] packages) {
177 clear();
178
179 ArrayList<ApplicationInfo> apps = new ArrayList<>();
180 for (int i = 0; i < packages.length; i++) {
181 try {
182 apps.add(mPm.getApplicationInfo(packages[i],
183 PackageManager.MATCH_KNOWN_PACKAGES));
184 } catch (PackageManager.NameNotFoundException e) {
185 }
186 }
187
188 apps.sort(new ApplicationInfo.DisplayNameComparator(mPm));
189 addAll(apps);
190 }
191
192 public @NonNull
193 View getView(int position, @Nullable View convertView,
194 @NonNull ViewGroup parent) {
195 final View view;
196 if (convertView == null) {
197 view = mInflater.inflate(R.layout.foreground_service_item, parent, false);
198 } else {
199 view = convertView;
200 }
201
202 ImageView icon = view.findViewById(R.id.app_icon);
Dianne Hackborn157a3f92017-05-01 12:33:31 -0700203 icon.setImageDrawable(mIconDrawableFactory.getBadgedIcon(getItem(position)));
Dianne Hackborn83b40f62017-04-26 13:59:47 -0700204
205 TextView label = view.findViewById(R.id.app_name);
206 label.setText(getItem(position).loadLabel(mPm));
207
208 return view;
209 }
210 }
211}