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.content.Intent; 22 import android.os.Bundle; 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_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME; 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 newInstance()38 public static SetupProfileFragment newInstance() { 39 return new SetupProfileFragment(); 40 } 41 SetupProfileFragment()42 public SetupProfileFragment() { 43 } 44 45 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)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 onViewCreated(View view, Bundle savedInstanceState)52 public void onViewCreated(View view, Bundle savedInstanceState) { 53 view.findViewById(R.id.set_up_profile).setOnClickListener(this); 54 } 55 56 @Override onClick(View view)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 */ provisionManagedProfile()70 private void provisionManagedProfile() { 71 Activity activity = getActivity(); 72 if (null == activity) { 73 return; 74 } 75 Intent intent = new Intent(ACTION_PROVISION_MANAGED_PROFILE); 76 intent.putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, 77 activity.getApplicationContext().getPackageName()); 78 if (intent.resolveActivity(activity.getPackageManager()) != null) { 79 startActivityForResult(intent, REQUEST_PROVISION_MANAGED_PROFILE); 80 activity.finish(); 81 } else { 82 Toast.makeText(activity, "Device provisioning is not enabled. Stopping.", 83 Toast.LENGTH_SHORT).show(); 84 } 85 } 86 87 @Override onActivityResult(int requestCode, int resultCode, Intent data)88 public void onActivityResult(int requestCode, int resultCode, Intent data) { 89 if (requestCode == REQUEST_PROVISION_MANAGED_PROFILE) { 90 if (resultCode == Activity.RESULT_OK) { 91 Toast.makeText(getActivity(), "Provisioning done.", Toast.LENGTH_SHORT).show(); 92 } else { 93 Toast.makeText(getActivity(), "Provisioning failed.", Toast.LENGTH_SHORT).show(); 94 } 95 return; 96 } 97 super.onActivityResult(requestCode, resultCode, data); 98 } 99 100 } 101