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