Allow OnCancelListener and OnDismissListener in Settings app
for the framework-managed dialogs. DialogFragment acts as both listeners so the application cannot set both listeners in the embedded dialog. New hooks are added in SettingsDialogFragment so that settings apps can do so for the framework-managed dialogs. Bug: 3386670 Change-Id: I144e7c4ccf7f86c61f6079fa86d830c709335af1
This commit is contained in:
@@ -21,6 +21,7 @@ import android.app.Dialog;
|
|||||||
import android.app.DialogFragment;
|
import android.app.DialogFragment;
|
||||||
import android.app.Fragment;
|
import android.app.Fragment;
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
@@ -111,6 +112,28 @@ public class SettingsPreferenceFragment extends PreferenceFragment
|
|||||||
mDialogFragment = null;
|
mDialogFragment = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the OnCancelListener of the dialog shown. This method can only be
|
||||||
|
* called after showDialog(int) and before removeDialog(int). The method
|
||||||
|
* does nothing otherwise.
|
||||||
|
*/
|
||||||
|
protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
|
||||||
|
if (mDialogFragment != null) {
|
||||||
|
mDialogFragment.mOnCancelListener = listener;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the OnDismissListener of the dialog shown. This method can only be
|
||||||
|
* called after showDialog(int) and before removeDialog(int). The method
|
||||||
|
* does nothing otherwise.
|
||||||
|
*/
|
||||||
|
protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
|
||||||
|
if (mDialogFragment != null) {
|
||||||
|
mDialogFragment.mOnDismissListener = listener;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static class SettingsDialogFragment extends DialogFragment {
|
public static class SettingsDialogFragment extends DialogFragment {
|
||||||
private static final String KEY_DIALOG_ID = "key_dialog_id";
|
private static final String KEY_DIALOG_ID = "key_dialog_id";
|
||||||
private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
|
private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
|
||||||
@@ -119,6 +142,9 @@ public class SettingsPreferenceFragment extends PreferenceFragment
|
|||||||
|
|
||||||
private Fragment mParentFragment;
|
private Fragment mParentFragment;
|
||||||
|
|
||||||
|
private DialogInterface.OnCancelListener mOnCancelListener;
|
||||||
|
private DialogInterface.OnDismissListener mOnDismissListener;
|
||||||
|
|
||||||
public SettingsDialogFragment() {
|
public SettingsDialogFragment() {
|
||||||
/* do nothing */
|
/* do nothing */
|
||||||
}
|
}
|
||||||
@@ -163,6 +189,21 @@ public class SettingsPreferenceFragment extends PreferenceFragment
|
|||||||
return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
|
return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCancel(DialogInterface dialog) {
|
||||||
|
super.onCancel(dialog);
|
||||||
|
if (mOnCancelListener != null) {
|
||||||
|
mOnCancelListener.onCancel(dialog);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDismiss(DialogInterface dialog) {
|
||||||
|
super.onDismiss(dialog);
|
||||||
|
if (mOnDismissListener != null) {
|
||||||
|
mOnDismissListener.onDismiss(dialog);
|
||||||
|
}
|
||||||
|
}
|
||||||
public int getDialogId() {
|
public int getDialogId() {
|
||||||
return mDialogId;
|
return mDialogId;
|
||||||
}
|
}
|
||||||
|
@@ -311,19 +311,29 @@ public class Memory extends SettingsPreferenceFragment implements OnCancelListen
|
|||||||
}})
|
}})
|
||||||
.setNegativeButton(R.string.cancel, null)
|
.setNegativeButton(R.string.cancel, null)
|
||||||
.setMessage(R.string.dlg_confirm_unmount_text)
|
.setMessage(R.string.dlg_confirm_unmount_text)
|
||||||
.setOnCancelListener(this)
|
|
||||||
.create();
|
.create();
|
||||||
case DLG_ERROR_UNMOUNT:
|
case DLG_ERROR_UNMOUNT:
|
||||||
return new AlertDialog.Builder(getActivity())
|
return new AlertDialog.Builder(getActivity())
|
||||||
.setTitle(R.string.dlg_error_unmount_title)
|
.setTitle(R.string.dlg_error_unmount_title)
|
||||||
.setNeutralButton(R.string.dlg_ok, null)
|
.setNeutralButton(R.string.dlg_ok, null)
|
||||||
.setMessage(R.string.dlg_error_unmount_text)
|
.setMessage(R.string.dlg_error_unmount_text)
|
||||||
.setOnCancelListener(this)
|
|
||||||
.create();
|
.create();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void showDialog(int id) {
|
||||||
|
super.showDialog(id);
|
||||||
|
|
||||||
|
switch (id) {
|
||||||
|
case DLG_CONFIRM_UNMOUNT:
|
||||||
|
case DLG_ERROR_UNMOUNT:
|
||||||
|
setOnCancelListener(this);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void doUnmount(boolean force) {
|
private void doUnmount(boolean force) {
|
||||||
// Present a toast here
|
// Present a toast here
|
||||||
Toast.makeText(getActivity(), R.string.unmount_inform_text, Toast.LENGTH_SHORT).show();
|
Toast.makeText(getActivity(), R.string.unmount_inform_text, Toast.LENGTH_SHORT).show();
|
||||||
|
@@ -238,6 +238,17 @@ public class VpnSettings extends SettingsPreferenceFragment
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void showDialog(int dialogId) {
|
||||||
|
super.showDialog(dialogId);
|
||||||
|
|
||||||
|
setOnCancelListener(new DialogInterface.OnCancelListener() {
|
||||||
|
public void onCancel(DialogInterface dialog) {
|
||||||
|
onIdle();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Dialog onCreateDialog (int id) {
|
public Dialog onCreateDialog (int id) {
|
||||||
switch (id) {
|
switch (id) {
|
||||||
@@ -267,27 +278,17 @@ public class VpnSettings extends SettingsPreferenceFragment
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class ConnectDialog extends AlertDialog {
|
|
||||||
public ConnectDialog(Context context) {
|
|
||||||
super(context);
|
|
||||||
setTitle(String.format(getString(R.string.vpn_connect_to),
|
|
||||||
mConnectingActor.getProfile().getName()));
|
|
||||||
setButton(DialogInterface.BUTTON_POSITIVE,
|
|
||||||
getString(R.string.vpn_connect_button),
|
|
||||||
VpnSettings.this);
|
|
||||||
setButton(DialogInterface.BUTTON_NEGATIVE,
|
|
||||||
getString(android.R.string.cancel),
|
|
||||||
VpnSettings.this);
|
|
||||||
setView(mConnectingActor.createConnectView());
|
|
||||||
}
|
|
||||||
public void onBackPressed() {
|
|
||||||
changeState(mActiveProfile, VpnState.IDLE);
|
|
||||||
super.onBackPressed();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Dialog createConnectDialog() {
|
private Dialog createConnectDialog() {
|
||||||
return new ConnectDialog(getActivity());
|
final Activity activity = getActivity();
|
||||||
|
return new AlertDialog.Builder(activity)
|
||||||
|
.setView(mConnectingActor.createConnectView())
|
||||||
|
.setTitle(String.format(activity.getString(R.string.vpn_connect_to),
|
||||||
|
mConnectingActor.getProfile().getName()))
|
||||||
|
.setPositiveButton(activity.getString(R.string.vpn_connect_button),
|
||||||
|
this)
|
||||||
|
.setNegativeButton(activity.getString(android.R.string.cancel),
|
||||||
|
this)
|
||||||
|
.create();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Dialog createReconnectDialog(int id) {
|
private Dialog createReconnectDialog(int id) {
|
||||||
@@ -375,11 +376,6 @@ public class VpnSettings extends SettingsPreferenceFragment
|
|||||||
public void onClick(DialogInterface dialog, int w) {
|
public void onClick(DialogInterface dialog, int w) {
|
||||||
onIdle();
|
onIdle();
|
||||||
}
|
}
|
||||||
})
|
|
||||||
.setOnCancelListener(new DialogInterface.OnCancelListener() {
|
|
||||||
public void onCancel(DialogInterface dialog) {
|
|
||||||
onIdle();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user