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.admin.DevicePolicyManager; 20 import android.content.Context; 21 import android.content.pm.ApplicationInfo; 22 import android.content.pm.PackageManager; 23 import android.os.Bundle; 24 import android.support.v4.app.FragmentActivity; 25 26 public class MainActivity extends FragmentActivity implements StatusFragment.StatusUpdatedListener { 27 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_main_real); 32 if (null == savedInstanceState) { 33 DevicePolicyManager devicePolicyManager = 34 (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); 35 PackageManager packageManager = getPackageManager(); 36 if (!devicePolicyManager.isProfileOwnerApp(getApplicationContext().getPackageName())) { 37 // If the managed profile is not yet set up, we show the setup screen. 38 showSetupProfile(); 39 } else { 40 try { 41 ApplicationInfo info = packageManager.getApplicationInfo( 42 Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA, 43 PackageManager.GET_UNINSTALLED_PACKAGES); 44 if (0 == (info.flags & ApplicationInfo.FLAG_INSTALLED)) { 45 // Need to reinstall the sample app 46 showStatusProfile(); 47 } else if (devicePolicyManager.isApplicationHidden( 48 EnforcerDeviceAdminReceiver.getComponentName(this), 49 Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA)) { 50 // The app is installed but hidden in this profile 51 showStatusProfile(); 52 } else { 53 // Everything is clear; show the main screen 54 showMainFragment(); 55 } 56 } catch (PackageManager.NameNotFoundException e) { 57 showStatusProfile(); 58 } 59 } 60 } 61 } 62 63 private void showSetupProfile() { 64 getSupportFragmentManager().beginTransaction() 65 .replace(R.id.container, new SetupProfileFragment()) 66 .commit(); 67 } 68 69 private void showStatusProfile() { 70 getSupportFragmentManager().beginTransaction() 71 .replace(R.id.container, new StatusFragment()) 72 .commit(); 73 } 74 75 private void showMainFragment() { 76 getSupportFragmentManager().beginTransaction() 77 .replace(R.id.container, new AppRestrictionEnforcerFragment()) 78 .commit(); 79 } 80 81 @Override 82 public void onStatusUpdated() { 83 showMainFragment(); 84 } 85 86 } 87