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