Add browseable samples for L SDK release
Change-Id: I71c6ff9a90b7734042d68af7f01e6d61118cc508
This commit is contained in:
@@ -0,0 +1,231 @@
|
||||
/*
|
||||
* Copyright (C) 2014 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.apprestrictionenforcer;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.admin.DevicePolicyManager;
|
||||
import android.content.Context;
|
||||
import android.content.RestrictionEntry;
|
||||
import android.content.RestrictionsManager;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.Switch;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This fragment provides UI and functionality to set restrictions on the AppRestrictionSchema
|
||||
* sample.
|
||||
*/
|
||||
public class AppRestrictionEnforcerFragment extends Fragment implements View.OnClickListener,
|
||||
CompoundButton.OnCheckedChangeListener {
|
||||
|
||||
/**
|
||||
* Package name of the AppRestrictionSchema sample.
|
||||
*/
|
||||
private static final String PACKAGE_NAME_APP_RESTRICTION_SCHEMA
|
||||
= "com.example.android.apprestrictionschema";
|
||||
|
||||
/**
|
||||
* Key for {@link SharedPreferences}
|
||||
*/
|
||||
private static final String PREFS_KEY = "AppRestrictionEnforcerFragment";
|
||||
|
||||
/**
|
||||
* Key for the boolean restriction in AppRestrictionSchema.
|
||||
*/
|
||||
private static final String RESTRICTION_KEY_SAY_HELLO = "can_say_hello";
|
||||
|
||||
/**
|
||||
* Default boolean value for "can_say_hello" restriction. The actual value is loaded in
|
||||
* {@link #loadRestrictions(android.app.Activity)}.
|
||||
*/
|
||||
private boolean mDefaultValueRestrictionSayHello;
|
||||
|
||||
// UI Components
|
||||
private TextView mTextStatus;
|
||||
private Button mButtonUnhide;
|
||||
private Switch mSwitchSayHello;
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.fragment_app_restriction_enforcer, container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
|
||||
mTextStatus = (TextView) view.findViewById(R.id.status);
|
||||
mButtonUnhide = (Button) view.findViewById(R.id.unhide);
|
||||
mSwitchSayHello = (Switch) view.findViewById(R.id.say_hello);
|
||||
mButtonUnhide.setOnClickListener(this);
|
||||
mSwitchSayHello.setOnCheckedChangeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
updateUi(getActivity());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
switch (view.getId()) {
|
||||
case R.id.unhide: {
|
||||
unhideApp(getActivity());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
|
||||
switch (compoundButton.getId()) {
|
||||
case R.id.say_hello: {
|
||||
allowSayHello(getActivity(), checked);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the UI components according to the current status of AppRestrictionSchema and its
|
||||
* restriction.
|
||||
*
|
||||
* @param activity The activity
|
||||
*/
|
||||
private void updateUi(Activity activity) {
|
||||
PackageManager packageManager = activity.getPackageManager();
|
||||
try {
|
||||
ApplicationInfo info = packageManager.getApplicationInfo(
|
||||
PACKAGE_NAME_APP_RESTRICTION_SCHEMA, PackageManager.GET_UNINSTALLED_PACKAGES);
|
||||
DevicePolicyManager devicePolicyManager =
|
||||
(DevicePolicyManager) activity.getSystemService(Activity.DEVICE_POLICY_SERVICE);
|
||||
if (0 < (info.flags & ApplicationInfo.FLAG_INSTALLED)) {
|
||||
if (!devicePolicyManager.isApplicationHidden(
|
||||
EnforcerDeviceAdminReceiver.getComponentName(activity),
|
||||
PACKAGE_NAME_APP_RESTRICTION_SCHEMA)) {
|
||||
// The app is ready
|
||||
loadRestrictions(activity);
|
||||
mTextStatus.setVisibility(View.GONE);
|
||||
mButtonUnhide.setVisibility(View.GONE);
|
||||
mSwitchSayHello.setVisibility(View.VISIBLE);
|
||||
mSwitchSayHello.setOnCheckedChangeListener(null);
|
||||
mSwitchSayHello.setChecked(canSayHello(activity));
|
||||
mSwitchSayHello.setOnCheckedChangeListener(this);
|
||||
} else {
|
||||
// The app is installed but hidden in this profile
|
||||
mTextStatus.setText(R.string.status_not_activated);
|
||||
mTextStatus.setVisibility(View.VISIBLE);
|
||||
mButtonUnhide.setVisibility(View.VISIBLE);
|
||||
mSwitchSayHello.setVisibility(View.GONE);
|
||||
}
|
||||
} else {
|
||||
// Need to reinstall the sample app
|
||||
mTextStatus.setText(R.string.status_need_reinstall);
|
||||
mTextStatus.setVisibility(View.VISIBLE);
|
||||
mButtonUnhide.setVisibility(View.GONE);
|
||||
mSwitchSayHello.setVisibility(View.GONE);
|
||||
}
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
mTextStatus.setText(R.string.status_not_installed);
|
||||
mTextStatus.setVisibility(View.VISIBLE);
|
||||
mButtonUnhide.setVisibility(View.GONE);
|
||||
mSwitchSayHello.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unhides the AppRestrictionSchema sample in case it is hidden in this profile.
|
||||
*
|
||||
* @param activity The activity
|
||||
*/
|
||||
private void unhideApp(Activity activity) {
|
||||
DevicePolicyManager devicePolicyManager =
|
||||
(DevicePolicyManager) activity.getSystemService(Activity.DEVICE_POLICY_SERVICE);
|
||||
devicePolicyManager.setApplicationHidden(
|
||||
EnforcerDeviceAdminReceiver.getComponentName(activity),
|
||||
PACKAGE_NAME_APP_RESTRICTION_SCHEMA, false);
|
||||
Toast.makeText(activity, "Enabled the app", Toast.LENGTH_SHORT).show();
|
||||
updateUi(activity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the restrictions for the AppRestrictionSchema sample. In this implementation, we just
|
||||
* read the default value for the "can_say_hello" restriction.
|
||||
*
|
||||
* @param activity The activity
|
||||
*/
|
||||
private void loadRestrictions(Activity activity) {
|
||||
RestrictionsManager restrictionsManager =
|
||||
(RestrictionsManager) activity.getSystemService(Context.RESTRICTIONS_SERVICE);
|
||||
List<RestrictionEntry> restrictions =
|
||||
restrictionsManager.getManifestRestrictions(PACKAGE_NAME_APP_RESTRICTION_SCHEMA);
|
||||
for (RestrictionEntry restriction : restrictions) {
|
||||
if (RESTRICTION_KEY_SAY_HELLO.equals(restriction.getKey())) {
|
||||
mDefaultValueRestrictionSayHello = restriction.getSelectedState();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the AppRestrictionSchema is currently allowed to say hello to its user. Note
|
||||
* that a profile/device owner needs to remember each restriction value on its own.
|
||||
*
|
||||
* @param activity The activity
|
||||
* @return True if the AppRestrictionSchema is allowed to say hello
|
||||
*/
|
||||
private boolean canSayHello(Activity activity) {
|
||||
SharedPreferences prefs = activity.getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
|
||||
return prefs.getBoolean(RESTRICTION_KEY_SAY_HELLO, mDefaultValueRestrictionSayHello);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value for the "cay_say_hello" restriction of AppRestrictionSchema.
|
||||
*
|
||||
* @param activity The activity
|
||||
* @param allow The value to be set for the restriction.
|
||||
*/
|
||||
private void allowSayHello(Activity activity, boolean allow) {
|
||||
DevicePolicyManager devicePolicyManager
|
||||
= (DevicePolicyManager) activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
|
||||
Bundle restrictions = new Bundle();
|
||||
restrictions.putBoolean(RESTRICTION_KEY_SAY_HELLO, allow);
|
||||
devicePolicyManager.setApplicationRestrictions(
|
||||
EnforcerDeviceAdminReceiver.getComponentName(activity),
|
||||
PACKAGE_NAME_APP_RESTRICTION_SCHEMA, restrictions);
|
||||
// The profile/device owner needs to remember the current state of restrictions on its own
|
||||
activity.getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE)
|
||||
.edit()
|
||||
.putBoolean(RESTRICTION_KEY_SAY_HELLO, allow)
|
||||
.apply();
|
||||
Toast.makeText(activity, allow ? R.string.allowed : R.string.disallowed,
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (C) 2014 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.apprestrictionenforcer;
|
||||
|
||||
import android.app.admin.DevicePolicyManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
|
||||
/**
|
||||
* This activity is started after the provisioning is complete in
|
||||
* {@link EnforcerDeviceAdminReceiver}.
|
||||
*/
|
||||
public class EnableProfileActivity extends FragmentActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
if (null == savedInstanceState) {
|
||||
// Enable the newly created profile
|
||||
DevicePolicyManager manager =
|
||||
(DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
|
||||
ComponentName componentName = EnforcerDeviceAdminReceiver.getComponentName(this);
|
||||
manager.setProfileName(componentName, getString(R.string.profile_name));
|
||||
manager.setProfileEnabled(componentName);
|
||||
}
|
||||
// Open the main screen
|
||||
startActivity(new Intent(this, MainActivity.class));
|
||||
finish();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (C) 2014 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.apprestrictionenforcer;
|
||||
|
||||
import android.app.admin.DeviceAdminReceiver;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
/**
|
||||
* Handles events related to managed profile.
|
||||
*/
|
||||
public class EnforcerDeviceAdminReceiver extends DeviceAdminReceiver {
|
||||
|
||||
/**
|
||||
* Called on the new profile when managed profile provisioning has completed. Managed profile
|
||||
* provisioning is the process of setting up the device so that it has a separate profile which
|
||||
* is managed by the mobile device management(mdm) application that triggered the provisioning.
|
||||
* Note that the managed profile is not fully visible until it is enabled.
|
||||
*/
|
||||
@Override
|
||||
public void onProfileProvisioningComplete(Context context, Intent intent) {
|
||||
// EnableProfileActivity is launched with the newly set up profile.
|
||||
Intent launch = new Intent(context, EnableProfileActivity.class);
|
||||
launch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
context.startActivity(launch);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a {@link ComponentName} that is used throughout the app.
|
||||
* @return a {@link ComponentName}
|
||||
*/
|
||||
public static ComponentName getComponentName(Context context) {
|
||||
return new ComponentName(context.getApplicationContext(),
|
||||
EnforcerDeviceAdminReceiver.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (C) 2014 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.apprestrictionenforcer;
|
||||
|
||||
import android.app.admin.DevicePolicyManager;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
|
||||
public class MainActivity extends FragmentActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main_real);
|
||||
if (null == savedInstanceState) {
|
||||
DevicePolicyManager manager = (DevicePolicyManager)
|
||||
getSystemService(Context.DEVICE_POLICY_SERVICE);
|
||||
if (manager.isProfileOwnerApp(getApplicationContext().getPackageName())) {
|
||||
// If the managed profile is already set up, we show the main screen.
|
||||
showMainFragment();
|
||||
} else {
|
||||
// If not, we show the set up screen.
|
||||
showSetupProfile();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void showSetupProfile() {
|
||||
getSupportFragmentManager().beginTransaction()
|
||||
.replace(R.id.container, new SetupProfileFragment())
|
||||
.commit();
|
||||
}
|
||||
|
||||
private void showMainFragment() {
|
||||
getSupportFragmentManager().beginTransaction()
|
||||
.replace(R.id.container, new AppRestrictionEnforcerFragment())
|
||||
.commit();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright (C) 2014 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.apprestrictionenforcer;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Toast;
|
||||
|
||||
import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE;
|
||||
import static android.app.admin.DevicePolicyManager.EXTRA_DEVICE_ADMIN;
|
||||
import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME;
|
||||
|
||||
/**
|
||||
* This {@link Fragment} handles initiation of managed profile provisioning.
|
||||
*/
|
||||
public class SetupProfileFragment extends Fragment implements View.OnClickListener {
|
||||
|
||||
private static final int REQUEST_PROVISION_MANAGED_PROFILE = 1;
|
||||
|
||||
public static SetupProfileFragment newInstance() {
|
||||
return new SetupProfileFragment();
|
||||
}
|
||||
|
||||
public SetupProfileFragment() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.fragment_setup_profile, container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||
view.findViewById(R.id.set_up_profile).setOnClickListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
switch (view.getId()) {
|
||||
case R.id.set_up_profile: {
|
||||
provisionManagedProfile();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates the managed profile provisioning. If we already have a managed profile set up on
|
||||
* this device, we will get an error dialog in the following provisioning phase.
|
||||
*/
|
||||
private void provisionManagedProfile() {
|
||||
Activity activity = getActivity();
|
||||
if (null == activity) {
|
||||
return;
|
||||
}
|
||||
Intent intent = new Intent(ACTION_PROVISION_MANAGED_PROFILE);
|
||||
intent.putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME,
|
||||
activity.getApplicationContext().getPackageName());
|
||||
intent.putExtra(EXTRA_DEVICE_ADMIN, EnforcerDeviceAdminReceiver.getComponentName(activity));
|
||||
if (intent.resolveActivity(activity.getPackageManager()) != null) {
|
||||
startActivityForResult(intent, REQUEST_PROVISION_MANAGED_PROFILE);
|
||||
activity.finish();
|
||||
} else {
|
||||
Toast.makeText(activity, "Device provisioning is not enabled. Stopping.",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (requestCode == REQUEST_PROVISION_MANAGED_PROFILE) {
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
Toast.makeText(getActivity(), "Provisioning done.", Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
Toast.makeText(getActivity(), "Provisioning failed.", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
return;
|
||||
}
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user