Files
app_Settings/src/com/android/settings/fuelgauge/ButtonActionDialogFragment.java
jackqdyulei 31b8de1e21 Create Controller for app buttons
These two buttons(uninstall + forcestop) are used in both battery page
and app page, we should move the logic for these two buttons into one
place.

This cl creates the AppButtonsPreferenceController for the above
purpose. This cl only copies the logic to controller but hasn't make
InstalledAppDetails use this controller.

Since DialogFragment could not use function in controller directly,
the controller expose DialogListener and all the fragments must
implement this interface. Then they can delegate the method call
to controller directly.

The following cl will:
1. Make the InstalledAppDetails be compatible to controller
2. Make the InstalledAppDetails use this controller.

Bug: 35810915
Test: RunSettingsRoboTests
Change-Id: Ie2aa8064bcec3003233896c18be772825b12930a
(cherry picked from commit 82d07983b4)
2017-03-29 20:21:37 +00:00

105 lines
3.4 KiB
Java

package com.android.settings.fuelgauge;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.IntDef;
import android.support.annotation.VisibleForTesting;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Fragment to show the dialog for uninstall or forcestop. This fragment uses function in
* target fragment to handle the dialog button click.
*/
public class ButtonActionDialogFragment extends InstrumentedDialogFragment implements
DialogInterface.OnClickListener {
/**
* Interface to handle the dialog click
*/
interface AppButtonsDialogListener {
void handleDialogClick(int type);
}
@Retention(RetentionPolicy.SOURCE)
@IntDef({
DialogType.DISABLE,
DialogType.SPECIAL_DISABLE,
DialogType.FORCE_STOP
})
public @interface DialogType {
int DISABLE = 0;
int SPECIAL_DISABLE = 1;
int FORCE_STOP = 2;
}
private static final String ARG_ID = "id";
@VisibleForTesting
int mId;
public static ButtonActionDialogFragment newInstance(@DialogType int id) {
ButtonActionDialogFragment dialogFragment = new ButtonActionDialogFragment();
Bundle args = new Bundle(1);
args.putInt(ARG_ID, id);
dialogFragment.setArguments(args);
return dialogFragment;
}
@Override
public int getMetricsCategory() {
//TODO(35810915): update the metrics label because for now this fragment will be shown
// in two screens
return MetricsProto.MetricsEvent.DIALOG_APP_INFO_ACTION;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Bundle bundle = getArguments();
mId = bundle.getInt(ARG_ID);
Dialog dialog = createDialog(mId);
if (dialog == null) {
throw new IllegalArgumentException("unknown id " + mId);
}
return dialog;
}
@Override
public void onClick(DialogInterface dialog, int which) {
final AppButtonsDialogListener lsn =
(AppButtonsDialogListener) getTargetFragment();
lsn.handleDialogClick(mId);
}
private AlertDialog createDialog(int id) {
final Context context = getContext();
switch (id) {
case DialogType.DISABLE:
case DialogType.SPECIAL_DISABLE:
return new AlertDialog.Builder(context)
.setMessage(R.string.app_disable_dlg_text)
.setPositiveButton(R.string.app_disable_dlg_positive, this)
.setNegativeButton(R.string.dlg_cancel, null)
.create();
case DialogType.FORCE_STOP:
return new AlertDialog.Builder(context)
.setTitle(R.string.force_stop_dlg_title)
.setMessage(R.string.force_stop_dlg_text)
.setPositiveButton(R.string.dlg_ok, this)
.setNegativeButton(R.string.dlg_cancel, null)
.create();
}
return null;
}
}