Convert credential view to DialogFragment
Bug: 26352310 Change-Id: Ic6af37c893eec5a83e198b9f23ebd766e88e0109
This commit is contained in:
@@ -17,10 +17,16 @@
|
|||||||
package com.android.settings;
|
package com.android.settings;
|
||||||
|
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
|
import android.app.Dialog;
|
||||||
|
import android.app.DialogFragment;
|
||||||
|
import android.app.Fragment;
|
||||||
|
import android.app.FragmentManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Parcel;
|
||||||
|
import android.os.Parcelable;
|
||||||
import android.security.Credentials;
|
import android.security.Credentials;
|
||||||
import android.security.KeyStore;
|
import android.security.KeyStore;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
@@ -29,7 +35,6 @@ import android.view.ViewGroup;
|
|||||||
import android.widget.AdapterView;
|
import android.widget.AdapterView;
|
||||||
import android.widget.AdapterView.OnItemClickListener;
|
import android.widget.AdapterView.OnItemClickListener;
|
||||||
import android.widget.ArrayAdapter;
|
import android.widget.ArrayAdapter;
|
||||||
import android.widget.ListAdapter;
|
|
||||||
import android.widget.ListView;
|
import android.widget.ListView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
@@ -56,7 +61,7 @@ public class UserCredentialsSettings extends InstrumentedFragment implements OnI
|
|||||||
@Override
|
@Override
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
new AliasLoader().execute();
|
refreshItems();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -74,26 +79,62 @@ public class UserCredentialsSettings extends InstrumentedFragment implements OnI
|
|||||||
@Override
|
@Override
|
||||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||||
final Credential item = (Credential) parent.getItemAtPosition(position);
|
final Credential item = (Credential) parent.getItemAtPosition(position);
|
||||||
|
CredentialDialogFragment.show(this, item);
|
||||||
|
}
|
||||||
|
|
||||||
View root = getActivity().getLayoutInflater()
|
protected void refreshItems() {
|
||||||
.inflate(R.layout.user_credential_dialog, null);
|
if (isAdded()) {
|
||||||
ViewGroup infoContainer = (ViewGroup) root.findViewById(R.id.credential_container);
|
new AliasLoader().execute();
|
||||||
infoContainer.addView(parent.getAdapter().getView(position, null, null));
|
}
|
||||||
|
}
|
||||||
|
|
||||||
new AlertDialog.Builder(getActivity())
|
public static class CredentialDialogFragment extends DialogFragment {
|
||||||
.setView(root)
|
private static final String TAG = "CredentialDialogFragment";
|
||||||
.setTitle(R.string.user_credential_title)
|
private static final String ARG_CREDENTIAL = "credential";
|
||||||
.setPositiveButton(R.string.done, null)
|
|
||||||
.setNegativeButton(R.string.trusted_credentials_remove_label,
|
public static void show(Fragment target, Credential item) {
|
||||||
new DialogInterface.OnClickListener() {
|
final Bundle args = new Bundle();
|
||||||
@Override public void onClick(DialogInterface dialog, int id) {
|
args.putParcelable(ARG_CREDENTIAL, item);
|
||||||
final KeyStore ks = KeyStore.getInstance();
|
|
||||||
Credentials.deleteAllTypesForAlias(ks, item.alias);
|
final CredentialDialogFragment frag = new CredentialDialogFragment();
|
||||||
new AliasLoader().execute();
|
frag.setTargetFragment(target, /* requestCode */ -1);
|
||||||
dialog.dismiss();
|
frag.setArguments(args);
|
||||||
}
|
frag.show(target.getFragmentManager(), TAG);
|
||||||
})
|
}
|
||||||
.show();
|
|
||||||
|
@Override
|
||||||
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||||
|
final Credential item = (Credential) getArguments().getParcelable(ARG_CREDENTIAL);
|
||||||
|
View root = getActivity().getLayoutInflater()
|
||||||
|
.inflate(R.layout.user_credential_dialog, null);
|
||||||
|
ViewGroup infoContainer = (ViewGroup) root.findViewById(R.id.credential_container);
|
||||||
|
View view = new CredentialAdapter(getActivity(), R.layout.user_credential,
|
||||||
|
new Credential[] {item}).getView(0, null, null);
|
||||||
|
infoContainer.addView(view);
|
||||||
|
|
||||||
|
return new AlertDialog.Builder(getActivity())
|
||||||
|
.setView(root)
|
||||||
|
.setTitle(R.string.user_credential_title)
|
||||||
|
.setPositiveButton(R.string.done, null)
|
||||||
|
.setNegativeButton(R.string.trusted_credentials_remove_label,
|
||||||
|
new DialogInterface.OnClickListener() {
|
||||||
|
@Override public void onClick(DialogInterface dialog, int id) {
|
||||||
|
final KeyStore ks = KeyStore.getInstance();
|
||||||
|
Credentials.deleteAllTypesForAlias(ks, item.alias);
|
||||||
|
dialog.dismiss();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.create();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDismiss(DialogInterface dialog) {
|
||||||
|
final Fragment target = getTargetFragment();
|
||||||
|
if (target instanceof UserCredentialsSettings) {
|
||||||
|
((UserCredentialsSettings) target).refreshItems();
|
||||||
|
}
|
||||||
|
super.onDismiss(dialog);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -101,9 +142,9 @@ public class UserCredentialsSettings extends InstrumentedFragment implements OnI
|
|||||||
* The credentials are stored in a {@link CredentialAdapter} attached to the main
|
* The credentials are stored in a {@link CredentialAdapter} attached to the main
|
||||||
* {@link ListView} in the fragment.
|
* {@link ListView} in the fragment.
|
||||||
*/
|
*/
|
||||||
private class AliasLoader extends AsyncTask<Void, Void, ListAdapter> {
|
private class AliasLoader extends AsyncTask<Void, Void, SortedMap<String, Credential>> {
|
||||||
@Override
|
@Override
|
||||||
protected ListAdapter doInBackground(Void... params) {
|
protected SortedMap<String, Credential> doInBackground(Void... params) {
|
||||||
// Create a list of names for credential sets, ordered by name.
|
// Create a list of names for credential sets, ordered by name.
|
||||||
SortedMap<String, Credential> credentials = new TreeMap<>();
|
SortedMap<String, Credential> credentials = new TreeMap<>();
|
||||||
KeyStore keyStore = KeyStore.getInstance();
|
KeyStore keyStore = KeyStore.getInstance();
|
||||||
@@ -116,15 +157,14 @@ public class UserCredentialsSettings extends InstrumentedFragment implements OnI
|
|||||||
c.storedTypes.add(type);
|
c.storedTypes.add(type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return credentials;
|
||||||
// Flatten to array so that the list can be presented via ArrayAdapter.
|
|
||||||
Credential[] results = credentials.values().toArray(new Credential[0]);
|
|
||||||
return new CredentialAdapter(getActivity(), R.layout.user_credential, results);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(ListAdapter credentials) {
|
protected void onPostExecute(SortedMap<String, Credential> credentials) {
|
||||||
mListView.setAdapter(credentials);
|
// Convert the results to an array and present them using an ArrayAdapter.
|
||||||
|
mListView.setAdapter(new CredentialAdapter(getContext(), R.layout.user_credential,
|
||||||
|
credentials.values().toArray(new Credential[0])));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,7 +194,7 @@ public class UserCredentialsSettings extends InstrumentedFragment implements OnI
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class Credential {
|
private static class Credential implements Parcelable {
|
||||||
private static enum Type {
|
private static enum Type {
|
||||||
CA_CERTIFICATE (Credentials.CA_CERTIFICATE),
|
CA_CERTIFICATE (Credentials.CA_CERTIFICATE),
|
||||||
USER_CERTIFICATE (Credentials.USER_CERTIFICATE),
|
USER_CERTIFICATE (Credentials.USER_CERTIFICATE),
|
||||||
@@ -183,10 +223,46 @@ public class UserCredentialsSettings extends InstrumentedFragment implements OnI
|
|||||||
* <li>{@link Credentials.USER_SECRET_KEY}</li>
|
* <li>{@link Credentials.USER_SECRET_KEY}</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
*/
|
*/
|
||||||
final Set<Type> storedTypes = EnumSet.noneOf(Type.class);
|
final EnumSet<Type> storedTypes = EnumSet.noneOf(Type.class);
|
||||||
|
|
||||||
Credential(final String alias) {
|
Credential(final String alias) {
|
||||||
this.alias = alias;
|
this.alias = alias;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Credential(Parcel in) {
|
||||||
|
this(in.readString());
|
||||||
|
|
||||||
|
long typeBits = in.readLong();
|
||||||
|
for (Type i : Type.values()) {
|
||||||
|
if ((typeBits & (1L << i.ordinal())) != 0L) {
|
||||||
|
storedTypes.add(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void writeToParcel(Parcel out, int flags) {
|
||||||
|
out.writeString(alias);
|
||||||
|
|
||||||
|
long typeBits = 0;
|
||||||
|
for (Type i : storedTypes) {
|
||||||
|
typeBits |= 1L << i.ordinal();
|
||||||
|
}
|
||||||
|
out.writeLong(typeBits);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int describeContents() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final Parcelable.Creator<Credential> CREATOR
|
||||||
|
= new Parcelable.Creator<Credential>() {
|
||||||
|
public Credential createFromParcel(Parcel in) {
|
||||||
|
return new Credential(in);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Credential[] newArray(int size) {
|
||||||
|
return new Credential[size];
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user