1 /*
2  * Copyright (C) 2014 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.example.android.apprestrictionenforcer;
18 
19 import android.app.Activity;
20 import android.app.admin.DevicePolicyManager;
21 import android.content.pm.ApplicationInfo;
22 import android.content.pm.PackageManager;
23 import android.os.Build;
24 import android.os.Bundle;
25 import android.support.annotation.Nullable;
26 import android.support.v4.app.Fragment;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.Button;
31 import android.widget.TextView;
32 import android.widget.Toast;
33 
34 /**
35  * Provides UI for enabling the target app in this profile. The status of the app can be
36  * uninstalled, hidden, or enabled depending on the situations. This fragment shows suitable
37  * controls for each status.
38  */
39 public class StatusFragment extends Fragment implements View.OnClickListener {
40 
41     private TextView mTextStatus;
42     private Button mButtonUnhide;
43     private StatusUpdatedListener mListener;
44 
45     @Override
onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)46     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
47                              @Nullable Bundle savedInstanceState) {
48         return inflater.inflate(R.layout.fragment_status, container, false);
49     }
50 
51     @Override
onViewCreated(View view, @Nullable Bundle savedInstanceState)52     public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
53         mTextStatus = (TextView) view.findViewById(R.id.status);
54         mButtonUnhide = (Button) view.findViewById(R.id.unhide);
55         mButtonUnhide.setOnClickListener(this);
56     }
57 
58     @Override
onAttach(Activity activity)59     public void onAttach(Activity activity) {
60         super.onAttach(activity);
61         mListener = (StatusUpdatedListener) activity;
62     }
63 
64     @Override
onDetach()65     public void onDetach() {
66         mListener = null;
67         super.onDetach();
68     }
69 
70     @Override
onResume()71     public void onResume() {
72         super.onResume();
73         updateUi(getActivity());
74     }
75 
76     @Override
onClick(View v)77     public void onClick(View v) {
78         switch (v.getId()) {
79             case R.id.unhide: {
80                 unhideApp(getActivity());
81                 break;
82             }
83         }
84     }
85 
updateUi(Activity activity)86     private void updateUi(Activity activity) {
87         PackageManager packageManager = activity.getPackageManager();
88         try {
89             int packageFlags;
90             if (Build.VERSION.SDK_INT < 24) {
91                 //noinspection deprecation
92                 packageFlags = PackageManager.GET_UNINSTALLED_PACKAGES;
93             } else {
94                 packageFlags = PackageManager.MATCH_UNINSTALLED_PACKAGES;
95             }
96             ApplicationInfo info = packageManager.getApplicationInfo(
97                     Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA,
98                     packageFlags);
99             DevicePolicyManager devicePolicyManager =
100                     (DevicePolicyManager) activity.getSystemService(Activity.DEVICE_POLICY_SERVICE);
101             if ((info.flags & ApplicationInfo.FLAG_INSTALLED) != 0) {
102                 if (!devicePolicyManager.isApplicationHidden(
103                         EnforcerDeviceAdminReceiver.getComponentName(activity),
104                         Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA)) {
105                     // The app is ready to enforce restrictions
106                     // This is unlikely to happen in this sample as unhideApp() handles it.
107                     mListener.onStatusUpdated();
108                 } else {
109                     // The app is installed but hidden in this profile
110                     mTextStatus.setText(R.string.status_not_activated);
111                     mButtonUnhide.setVisibility(View.VISIBLE);
112                 }
113             } else {
114                 // Need to reinstall the sample app
115                 mTextStatus.setText(R.string.status_need_reinstall);
116                 mButtonUnhide.setVisibility(View.GONE);
117             }
118         } catch (PackageManager.NameNotFoundException e) {
119             // Need to reinstall the sample app
120             mTextStatus.setText(R.string.status_need_reinstall);
121             mButtonUnhide.setVisibility(View.GONE);
122         }
123     }
124 
125     /**
126      * Unhides the AppRestrictionSchema sample in case it is hidden in this profile.
127      *
128      * @param activity The activity
129      */
unhideApp(Activity activity)130     private void unhideApp(Activity activity) {
131         DevicePolicyManager devicePolicyManager =
132                 (DevicePolicyManager) activity.getSystemService(Activity.DEVICE_POLICY_SERVICE);
133         devicePolicyManager.setApplicationHidden(
134                 EnforcerDeviceAdminReceiver.getComponentName(activity),
135                 Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA, false);
136         Toast.makeText(activity, "Enabled the app", Toast.LENGTH_SHORT).show();
137         mListener.onStatusUpdated();
138     }
139 
140     public interface StatusUpdatedListener {
onStatusUpdated()141         public void onStatusUpdated();
142     }
143 
144 }
145