|
| 1 | +/* |
| 2 | + * Copyright (C) 2015 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.afwsamples.testdpc.policy; |
| 18 | + |
| 19 | +import android.app.Activity; |
| 20 | +import android.app.AlertDialog; |
| 21 | +import android.content.pm.PackageManager; |
| 22 | +import android.content.pm.ResolveInfo; |
| 23 | +import android.os.AsyncTask; |
| 24 | +import android.view.View; |
| 25 | +import android.widget.AdapterView; |
| 26 | +import android.widget.Button; |
| 27 | +import android.widget.ListView; |
| 28 | + |
| 29 | +import com.afwsamples.testdpc.R; |
| 30 | + |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.Collections; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +public abstract class GetAvailableComponentsTask<T> extends AsyncTask<Void, Void, List<T>> { |
| 36 | + private final Activity mActivity; |
| 37 | + private final int mTitleResId; |
| 38 | + |
| 39 | + private View mProgressView; |
| 40 | + private ListView mListView; |
| 41 | + private AlertDialog mAlertDialog; |
| 42 | + private Button mPositiveButton; |
| 43 | + private Button mNegativeButton; |
| 44 | + private Button mNeutralButton; |
| 45 | + |
| 46 | + public GetAvailableComponentsTask(Activity activity, int titleResId) { |
| 47 | + mActivity = activity; |
| 48 | + mTitleResId = titleResId; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + protected void onPreExecute() { |
| 53 | + View rootView = View.inflate(mActivity, R.layout.available_components_list, null); |
| 54 | + mAlertDialog = new AlertDialog.Builder(mActivity) |
| 55 | + .setTitle(mTitleResId) |
| 56 | + .setView(rootView) |
| 57 | + .setPositiveButton(android.R.string.ok, null) |
| 58 | + .setNeutralButton(R.string.allow_all, null) |
| 59 | + .setNegativeButton(android.R.string.cancel, null) |
| 60 | + .show(); |
| 61 | + mProgressView = rootView.findViewById(R.id.progress_layout); |
| 62 | + mListView = (ListView) rootView.findViewById(android.R.id.list); |
| 63 | + |
| 64 | + mPositiveButton = mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE); |
| 65 | + mNeutralButton = mAlertDialog.getButton(AlertDialog.BUTTON_NEUTRAL); |
| 66 | + mNegativeButton = mAlertDialog.getButton(AlertDialog.BUTTON_NEGATIVE); |
| 67 | + |
| 68 | + showProgressBar(true); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + protected void onPostExecute(List<T> availableComponentsList) { |
| 73 | + if (mActivity == null || mActivity.isFinishing()) { |
| 74 | + return; |
| 75 | + } |
| 76 | + final List<ResolveInfo> availableComponentsResolveInfoList = |
| 77 | + getResolveInfoListFromAvailableComponents(availableComponentsList); |
| 78 | + PackageManager packageManager = mActivity.getPackageManager(); |
| 79 | + Collections.sort(availableComponentsResolveInfoList, |
| 80 | + new ResolveInfo.DisplayNameComparator(packageManager)); |
| 81 | + final List<String> permittedComponentsList = getPermittedComponentsList(); |
| 82 | + final AvailableComponentsInfoArrayAdapter availableComponentsInfoArrayAdapter |
| 83 | + = new AvailableComponentsInfoArrayAdapter(mActivity, |
| 84 | + availableComponentsResolveInfoList, permittedComponentsList); |
| 85 | + mListView.setAdapter(availableComponentsInfoArrayAdapter); |
| 86 | + mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 87 | + @Override |
| 88 | + public void onItemClick(AdapterView<?> parent, View view, int pos, long id) { |
| 89 | + availableComponentsInfoArrayAdapter.onItemClick(parent, view, pos, id); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + showProgressBar(false); |
| 94 | + mPositiveButton.setOnClickListener(new View.OnClickListener() { |
| 95 | + @Override |
| 96 | + public void onClick(View view) { |
| 97 | + ArrayList<String> selectedComponents = |
| 98 | + availableComponentsInfoArrayAdapter.getSelectedComponents(); |
| 99 | + if (selectedComponents != null) { |
| 100 | + setPermittedComponentsList(selectedComponents); |
| 101 | + } |
| 102 | + mAlertDialog.dismiss(); |
| 103 | + } |
| 104 | + }); |
| 105 | + mNeutralButton.setOnClickListener(new View.OnClickListener() { |
| 106 | + @Override |
| 107 | + public void onClick(View view) { |
| 108 | + setPermittedComponentsList(null); |
| 109 | + mAlertDialog.dismiss(); |
| 110 | + } |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + private void showProgressBar(boolean show) { |
| 115 | + if (show) { |
| 116 | + mProgressView.setVisibility(View.VISIBLE); |
| 117 | + mListView.setVisibility(View.GONE); |
| 118 | + } else { |
| 119 | + mProgressView.setVisibility(View.GONE); |
| 120 | + mListView.setVisibility(View.VISIBLE); |
| 121 | + } |
| 122 | + mPositiveButton.setEnabled(!show); |
| 123 | + mNeutralButton.setEnabled(!show); |
| 124 | + mNegativeButton.setEnabled(!show); |
| 125 | + } |
| 126 | + |
| 127 | + protected abstract List<ResolveInfo> getResolveInfoListFromAvailableComponents( |
| 128 | + List<T> availableComponentsList); |
| 129 | + |
| 130 | + protected abstract List<String> getPermittedComponentsList(); |
| 131 | + |
| 132 | + protected abstract void setPermittedComponentsList(List<String> permittedComponentsList); |
| 133 | +} |
0 commit comments