Home | History | Annotate | Download | only in apprestrictionenforcer
      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.content.Intent;
     21 import android.os.Build;
     22 import android.os.Bundle;
     23 import android.support.v4.app.Fragment;
     24 import android.view.LayoutInflater;
     25 import android.view.View;
     26 import android.view.ViewGroup;
     27 import android.widget.Toast;
     28 
     29 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE;
     30 import static android.app.admin.DevicePolicyManager.EXTRA_DEVICE_ADMIN;
     31 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME;
     32 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME;
     33 
     34 /**
     35  * This {@link Fragment} handles initiation of managed profile provisioning.
     36  */
     37 public class SetupProfileFragment extends Fragment implements View.OnClickListener {
     38 
     39     private static final int REQUEST_PROVISION_MANAGED_PROFILE = 1;
     40 
     41     public static SetupProfileFragment newInstance() {
     42         return new SetupProfileFragment();
     43     }
     44 
     45     public SetupProfileFragment() {
     46     }
     47 
     48     @Override
     49     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     50                              Bundle savedInstanceState) {
     51         return inflater.inflate(R.layout.fragment_setup_profile, container, false);
     52     }
     53 
     54     @Override
     55     public void onViewCreated(View view, Bundle savedInstanceState) {
     56         view.findViewById(R.id.set_up_profile).setOnClickListener(this);
     57     }
     58 
     59     @Override
     60     public void onClick(View view) {
     61         switch (view.getId()) {
     62             case R.id.set_up_profile: {
     63                 provisionManagedProfile();
     64                 break;
     65             }
     66         }
     67     }
     68 
     69     /**
     70      * Initiates the managed profile provisioning. If we already have a managed profile set up on
     71      * this device, we will get an error dialog in the following provisioning phase.
     72      */
     73     private void provisionManagedProfile() {
     74         Activity activity = getActivity();
     75         if (null == activity) {
     76             return;
     77         }
     78         Intent intent = new Intent(ACTION_PROVISION_MANAGED_PROFILE);
     79         if (Build.VERSION.SDK_INT >= 24) {
     80             intent.putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME,
     81                     EnforcerDeviceAdminReceiver.getComponentName(activity));
     82         } else {
     83             //noinspection deprecation
     84             intent.putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME,
     85                     activity.getApplicationContext().getPackageName());
     86             intent.putExtra(EXTRA_DEVICE_ADMIN, EnforcerDeviceAdminReceiver.getComponentName(activity));
     87         }
     88         if (intent.resolveActivity(activity.getPackageManager()) != null) {
     89             startActivityForResult(intent, REQUEST_PROVISION_MANAGED_PROFILE);
     90             activity.finish();
     91         } else {
     92             Toast.makeText(activity, "Device provisioning is not enabled. Stopping.",
     93                     Toast.LENGTH_SHORT).show();
     94         }
     95     }
     96 
     97     @Override
     98     public void onActivityResult(int requestCode, int resultCode, Intent data) {
     99         if (requestCode == REQUEST_PROVISION_MANAGED_PROFILE) {
    100             if (resultCode == Activity.RESULT_OK) {
    101                 Toast.makeText(getActivity(), "Provisioning done.", Toast.LENGTH_SHORT).show();
    102             } else {
    103                 Toast.makeText(getActivity(), "Provisioning failed.", Toast.LENGTH_SHORT).show();
    104             }
    105             return;
    106         }
    107         super.onActivityResult(requestCode, resultCode, data);
    108     }
    109 
    110 }
    111