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