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