Skip to content

Commit f0473c6

Browse files
committed
Backport app restrictions managing package fragment [DO NOT MERGE]
Bug: 26111675 Change-Id: Ie49a14c3742a0892eae11b0dd91137aea94f8aa5
1 parent d65177a commit f0473c6

File tree

6 files changed

+285
-2
lines changed

6 files changed

+285
-2
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Copyright (C) 2016 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+
17+
package com.afwsamples.testdpc.common;
18+
19+
import android.app.Fragment;
20+
import android.content.pm.ApplicationInfo;
21+
import android.content.pm.PackageManager;
22+
import android.os.Bundle;
23+
import android.view.LayoutInflater;
24+
import android.view.View;
25+
import android.view.ViewGroup;
26+
import android.widget.AdapterView;
27+
import android.widget.AdapterView.OnItemClickListener;
28+
import android.widget.EditText;
29+
import android.widget.ListView;
30+
31+
import com.afwsamples.testdpc.R;
32+
33+
import java.util.ArrayList;
34+
import java.util.Collections;
35+
import java.util.List;
36+
37+
38+
/**
39+
* This fragment shows the list of apps, and allows the user to select one of them to perform a
40+
* certain operation as defined by {@link #setSelectedPackage}. Alternatively the user can manually
41+
* specify an app's package name, in case the app has not been installed yet.
42+
*/
43+
public abstract class SelectAppFragment extends Fragment implements View.OnClickListener,
44+
OnItemClickListener {
45+
46+
private EditText mCurrentSelectedPackage;
47+
private EditText mNewSelectedPackage;
48+
private ListView mAppList;
49+
private ArrayList<String> mAppPackages;
50+
51+
@Override
52+
public void onResume() {
53+
super.onResume();
54+
reloadSelectedPackage();
55+
}
56+
57+
@Override
58+
public void onCreate(Bundle savedInstanceState) {
59+
super.onCreate(savedInstanceState);
60+
mAppPackages = new ArrayList<String>();
61+
}
62+
63+
@Override
64+
public View onCreateView(LayoutInflater layoutInflater, ViewGroup container,
65+
Bundle savedInstanceState) {
66+
View view = layoutInflater.inflate(R.layout.select_app, null);
67+
68+
mCurrentSelectedPackage = (EditText) view.findViewById(R.id.selected_package_current);
69+
mNewSelectedPackage = (EditText) view.findViewById(R.id.selected_package_new);
70+
mAppList = (ListView) view.findViewById(R.id.select_app_list);
71+
populateApps();
72+
73+
view.findViewById(R.id.selected_package_set).setOnClickListener(this);
74+
view.findViewById(R.id.selected_package_clear).setOnClickListener(this);
75+
mAppList.setOnItemClickListener(this);
76+
77+
return view;
78+
}
79+
80+
private void populateApps() {
81+
PackageManager pm = getActivity().getPackageManager();
82+
List<ApplicationInfo> allApps = pm.getInstalledApplications(0 /* No flag */);
83+
Collections.sort(allApps, new ApplicationInfo.DisplayNameComparator(pm));
84+
mAppPackages.clear();
85+
for(ApplicationInfo info : allApps) {
86+
if ((pm.getLaunchIntentForPackage(info.packageName)) != null) {
87+
mAppPackages.add(info.packageName);
88+
}
89+
}
90+
AppInfoArrayAdapter appInfoArrayAdapter = new AppInfoArrayAdapter(getActivity(),
91+
R.id.pkg_name, mAppPackages, true);
92+
mAppList.setAdapter(appInfoArrayAdapter);
93+
}
94+
95+
96+
@Override
97+
public void onClick(View v) {
98+
switch (v.getId()) {
99+
case R.id.selected_package_set:
100+
setSelectedPackage(mNewSelectedPackage.getText().toString());
101+
reloadSelectedPackage();
102+
break;
103+
case R.id.selected_package_clear:
104+
mNewSelectedPackage.setText("");
105+
clearSelectedPackage();
106+
reloadSelectedPackage();
107+
break;
108+
}
109+
}
110+
111+
private void reloadSelectedPackage() {
112+
String selectedPackage = getSelectedPackage();
113+
if (selectedPackage == null) {
114+
mCurrentSelectedPackage.setText("");
115+
} else {
116+
mCurrentSelectedPackage.setText(selectedPackage);
117+
}
118+
}
119+
120+
@Override
121+
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
122+
String packageName = mAppPackages.get(position);
123+
mNewSelectedPackage.setText(packageName);
124+
}
125+
126+
/**
127+
* Sets the given package name as the new selected package.
128+
*/
129+
protected abstract void setSelectedPackage(String pkgName);
130+
131+
/**
132+
* Clears the currently selected package.
133+
*/
134+
protected abstract void clearSelectedPackage();
135+
136+
/**
137+
* Get the currently selected package, or {@code null} if no package has been set or if it has
138+
* been cleared.
139+
*/
140+
protected abstract String getSelectedPackage();
141+
}

app/src/main/java/com/afwsamples/testdpc/policy/PolicyManagementFragment.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
import com.afwsamples.testdpc.policy.wifimanagement.WifiModificationFragment;
8181
import com.afwsamples.testdpc.profilepolicy.ProfilePolicyManagementFragment;
8282
import com.afwsamples.testdpc.profilepolicy.addsystemapps.EnableSystemAppsByIntentFragment;
83+
import com.afwsamples.testdpc.profilepolicy.apprestrictions.AppRestrictionsManagingPackageFragment;
8384
import com.afwsamples.testdpc.profilepolicy.apprestrictions.ManageAppRestrictionsFragment;
8485
import com.afwsamples.testdpc.profilepolicy.permission.ManageAppPermissionsFragment;
8586

@@ -178,6 +179,8 @@ public class PolicyManagementFragment extends PreferenceFragment implements
178179

179180
public static final String OVERRIDE_KEY_SELECTION_KEY = "override_key_selection";
180181

182+
private static final String APP_RESTRICTIONS_MANAGING_PACKAGE_KEY
183+
= "app_restrictions_managing_package";
181184
private static final String BLOCK_UNINSTALLATION_BY_PKG_KEY = "block_uninstallation_by_pkg";
182185
private static final String BLOCK_UNINSTALLATION_LIST_KEY = "block_uninstallation_list";
183186
private static final String CAPTURE_IMAGE_KEY = "capture_image";
@@ -266,7 +269,7 @@ public class PolicyManagementFragment extends PreferenceFragment implements
266269
NETWORK_STATS_KEY, DELEGATED_CERT_INSTALLER_KEY, DISABLE_STATUS_BAR,
267270
REENABLE_STATUS_BAR, DISABLE_KEYGUARD, REENABLE_KEYGUARD, START_KIOSK_MODE,
268271
SET_PERMISSION_POLICY_KEY, MANAGE_APP_PERMISSIONS_KEY,STAY_ON_WHILE_PLUGGED_IN,
269-
WIFI_CONFIG_LOCKDOWN_ENABLE_KEY
272+
WIFI_CONFIG_LOCKDOWN_ENABLE_KEY, APP_RESTRICTIONS_MANAGING_PACKAGE_KEY
270273
};
271274

272275
/**
@@ -390,6 +393,7 @@ public void onCreate(Bundle savedInstanceState) {
390393
findPreference(HIDE_APPS_KEY).setOnPreferenceClickListener(this);
391394
findPreference(UNHIDE_APPS_KEY).setOnPreferenceClickListener(this);
392395
findPreference(MANAGE_APP_RESTRICTIONS_KEY).setOnPreferenceClickListener(this);
396+
findPreference(APP_RESTRICTIONS_MANAGING_PACKAGE_KEY).setOnPreferenceClickListener(this);
393397
findPreference(INSTALL_KEY_CERTIFICATE_KEY).setOnPreferenceClickListener(this);
394398
findPreference(INSTALL_CA_CERTIFICATE_KEY).setOnPreferenceClickListener(this);
395399
findPreference(GET_CA_CERTIFICATES_KEY).setOnPreferenceClickListener(this);
@@ -524,6 +528,9 @@ public void onPositiveButtonClicked(String[] lockTaskArray) {
524528
case MANAGE_APP_RESTRICTIONS_KEY:
525529
showFragment(new ManageAppRestrictionsFragment());
526530
return true;
531+
case APP_RESTRICTIONS_MANAGING_PACKAGE_KEY:
532+
showFragment(new AppRestrictionsManagingPackageFragment());
533+
return true;
527534
case SET_PERMISSION_POLICY_KEY:
528535
showSetPermissionPolicyDialog();
529536
return true;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (C) 2016 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+
17+
package com.afwsamples.testdpc.profilepolicy.apprestrictions;
18+
19+
import com.afwsamples.testdpc.R;
20+
import com.afwsamples.testdpc.common.SelectAppFragment;
21+
22+
/**
23+
* This fragment lets the user select an app that can manage application restrictions for the
24+
* current user. Related APIs:
25+
* 1) {@link AppRestrictionsProxyHandler#setApplicationRestrictionsManagingPackage}
26+
* 2) {@link AppRestrictionsProxyHandler#getApplicationRestrictionsManagingPackage}
27+
*/
28+
public class AppRestrictionsManagingPackageFragment extends SelectAppFragment {
29+
30+
@Override
31+
public void onResume() {
32+
super.onResume();
33+
getActivity().getActionBar().setTitle(R.string.app_restrictions_managing_package);
34+
}
35+
36+
@Override
37+
protected void setSelectedPackage(String pkgName) {
38+
AppRestrictionsProxyHandler.setApplicationRestrictionsManagingPackage(getContext(),
39+
pkgName);
40+
}
41+
42+
@Override
43+
protected void clearSelectedPackage() {
44+
AppRestrictionsProxyHandler.setApplicationRestrictionsManagingPackage(getContext(), null);
45+
}
46+
47+
@Override
48+
protected String getSelectedPackage() {
49+
return AppRestrictionsProxyHandler.getApplicationRestrictionsManagingPackage(getContext());
50+
}
51+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2016 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
19+
android:layout_width="match_parent"
20+
android:layout_height="match_parent"
21+
android:orientation="vertical">
22+
23+
<LinearLayout android:layout_width="match_parent"
24+
android:layout_height="wrap_content"
25+
android:orientation="horizontal">
26+
<TextView android:id="@+id/select_package_label"
27+
android:layout_width="wrap_content"
28+
android:layout_height="wrap_content"
29+
android:text="@string/current_selected_package"/>
30+
31+
<EditText
32+
android:id="@+id/selected_package_current"
33+
android:layout_width="fill_parent"
34+
android:layout_height="wrap_content"
35+
android:editable="false"
36+
android:hint="@string/selected_package_none"/>
37+
</LinearLayout>
38+
39+
<TextView android:layout_width="wrap_content"
40+
android:layout_height="wrap_content"
41+
android:text="@string/select_app_instructions"/>
42+
43+
<ListView
44+
android:id="@+id/select_app_list"
45+
android:scrollbars="vertical"
46+
android:layout_width="fill_parent"
47+
android:layout_height="0dp"
48+
android:layout_weight="1">
49+
</ListView>
50+
51+
<EditText
52+
android:id="@+id/selected_package_new"
53+
android:layout_width="fill_parent"
54+
android:layout_height="wrap_content"
55+
android:hint="@string/selected_package_new_label"/>
56+
57+
<LinearLayout
58+
android:layout_width="match_parent"
59+
android:layout_height="wrap_content"
60+
android:orientation="horizontal">
61+
62+
<Button android:id="@+id/selected_package_set"
63+
android:layout_width="90dip"
64+
android:layout_height="50dip"
65+
android:text="@string/selected_package_set" />
66+
67+
<Button android:id="@+id/selected_package_clear"
68+
android:layout_width="90dip"
69+
android:layout_height="50dip"
70+
android:text="@string/selected_package_clear" />
71+
</LinearLayout>
72+
</LinearLayout>

app/src/main/res/values/strings.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,15 @@
267267
<string name="ensure_verify_apps">Ensure verify apps</string>
268268
<string name="user_restriction_error_msg">Operation not allowed</string>
269269

270+
<!-- Strings for select app -->
271+
<string name="current_selected_package">Current package:</string>
272+
<string name="selected_package_new_label">New package name</string>
273+
<string name="selected_package_set">Set</string>
274+
<string name="selected_package_clear">Clear</string>
275+
<string name="selected_package_none">None</string>
276+
<string name="select_app_instructions">Select an app from the list,
277+
or manually enter a package name below:</string>
278+
270279
<!-- Strings for certificate management -->
271280
<string name="certificate_management_title">Certificate management</string>
272281
<string name="install_key_certificate">Install a private key certificate</string>
@@ -299,7 +308,7 @@
299308
<string name="set_app_restrictions_success">Successfully set app restrictions for %s.</string>
300309
<string name="app_restrictions_info">%1$s - %2$s</string>
301310
<string name="plus">+</string>
302-
311+
<string name="app_restrictions_managing_package">App restrictions manager</string>
303312

304313
<!-- strings used in the alert dialog for editing application restrictions. -->
305314
<string name="select_type">Select type:</string>

app/src/main/res/xml/device_policy_header.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
<Preference
5959
android:key="manage_app_restrictions"
6060
android:title="@string/manage_app_restrictions"/>
61+
<Preference
62+
android:key="app_restrictions_managing_package"
63+
android:title="@string/app_restrictions_managing_package"/>
6164
</PreferenceCategory>
6265

6366
<PreferenceCategory

0 commit comments

Comments
 (0)