change RemoteRegisterActivity into a dialog
This commit is contained in:
@@ -18,76 +18,173 @@
|
||||
package org.sufficientlysecure.keychain.remote.ui;
|
||||
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.app.Dialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.DialogFragment;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.view.ContextThemeWrapper;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.Button;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.remote.ui.RemoteRegisterPresenter.RemoteRegisterView;
|
||||
import org.sufficientlysecure.keychain.ui.base.BaseActivity;
|
||||
import org.sufficientlysecure.keychain.ui.dialog.CustomAlertDialogBuilder;
|
||||
import org.sufficientlysecure.keychain.ui.util.ThemeChanger;
|
||||
|
||||
|
||||
public class RemoteRegisterActivity extends BaseActivity implements RemoteRegisterView {
|
||||
|
||||
public class RemoteRegisterActivity extends FragmentActivity {
|
||||
public static final String EXTRA_PACKAGE_NAME = "package_name";
|
||||
public static final String EXTRA_PACKAGE_SIGNATURE = "package_signature";
|
||||
|
||||
public static final String EXTRA_DATA = "data";
|
||||
|
||||
private AppSettingsHeaderFragment mAppSettingsHeaderFragment;
|
||||
|
||||
private RemoteRegisterPresenter presenter;
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
this.presenter = new RemoteRegisterPresenter(getBaseContext());
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
RemoteRegisterDialogFragment frag = new RemoteRegisterDialogFragment();
|
||||
frag.show(getSupportFragmentManager(), "requestKeyDialog");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
|
||||
Intent intent = getIntent();
|
||||
Intent resultData = intent.getParcelableExtra(EXTRA_DATA);
|
||||
String packageName = intent.getStringExtra(EXTRA_PACKAGE_NAME);
|
||||
byte[] packageSignature = intent.getByteArrayExtra(EXTRA_PACKAGE_SIGNATURE);
|
||||
|
||||
presenter.setupFromIntent(resultData, packageName, packageSignature);
|
||||
presenter.setView(this);
|
||||
presenter.setupFromIntentData(resultData, packageName, packageSignature);
|
||||
}
|
||||
|
||||
mAppSettingsHeaderFragment = (AppSettingsHeaderFragment) getSupportFragmentManager().findFragmentById(
|
||||
R.id.api_app_settings_fragment);
|
||||
// TODO unclean, fix later
|
||||
mAppSettingsHeaderFragment.setAppSettings(presenter.appSettings);
|
||||
public static class RemoteRegisterDialogFragment extends DialogFragment {
|
||||
private RemoteRegisterPresenter presenter;
|
||||
private RemoteRegisterView mvpView;
|
||||
|
||||
setFullScreenDialogTwoButtons(
|
||||
R.string.api_register_allow, R.drawable.ic_check_white_24dp,
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
presenter.onClickAllow();
|
||||
}
|
||||
}, R.string.api_register_disallow, R.drawable.ic_close_white_24dp,
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
presenter.onClickCancel();
|
||||
private Button buttonAllow;
|
||||
private Button buttonCancel;
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
final Activity activity = getActivity();
|
||||
|
||||
ContextThemeWrapper theme = ThemeChanger.getDialogThemeWrapper(activity);
|
||||
CustomAlertDialogBuilder alert = new CustomAlertDialogBuilder(theme);
|
||||
|
||||
@SuppressLint("InflateParams")
|
||||
View view = LayoutInflater.from(theme).inflate(R.layout.api_remote_register_app, null, false);
|
||||
alert.setView(view);
|
||||
|
||||
buttonAllow = (Button) view.findViewById(R.id.button_allow);
|
||||
buttonCancel = (Button) view.findViewById(R.id.button_cancel);
|
||||
|
||||
setupListenersForPresenter();
|
||||
mvpView = createMvpView(view);
|
||||
|
||||
return alert.create();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
|
||||
presenter = ((RemoteRegisterActivity) getActivity()).presenter;
|
||||
presenter.setView(mvpView);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancel(DialogInterface dialog) {
|
||||
super.onCancel(dialog);
|
||||
|
||||
if (presenter != null) {
|
||||
presenter.onCancel();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDismiss(DialogInterface dialog) {
|
||||
super.onDismiss(dialog);
|
||||
|
||||
if (presenter != null) {
|
||||
presenter.setView(null);
|
||||
presenter = null;
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private RemoteRegisterView createMvpView(View view) {
|
||||
final TextView titleText = (TextView) view.findViewById(R.id.api_register_text);
|
||||
final ImageView iconClientApp = (ImageView) view.findViewById(R.id.icon_client_app);
|
||||
|
||||
return new RemoteRegisterView() {
|
||||
@Override
|
||||
public void finishWithResult(Intent resultIntent) {
|
||||
FragmentActivity activity = getActivity();
|
||||
if (activity == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
activity.setResult(RESULT_OK, resultIntent);
|
||||
activity.finish();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finishWithResult(Intent resultIntent) {
|
||||
setResult(RESULT_OK, resultIntent);
|
||||
finish();
|
||||
}
|
||||
@Override
|
||||
public void finishAsCancelled() {
|
||||
FragmentActivity activity = getActivity();
|
||||
if (activity == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finishAsCancelled() {
|
||||
setResult(RESULT_CANCELED);
|
||||
finish();
|
||||
}
|
||||
activity.setResult(RESULT_CANCELED);
|
||||
activity.finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initLayout() {
|
||||
setContentView(R.layout.api_remote_register_app);
|
||||
@Override
|
||||
public void setTitleText(String text) {
|
||||
titleText.setText(text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTitleClientIcon(Drawable drawable) {
|
||||
iconClientApp.setImageDrawable(drawable);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private void setupListenersForPresenter() {
|
||||
buttonAllow.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
presenter.onClickAllow();
|
||||
}
|
||||
});
|
||||
|
||||
buttonCancel.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
presenter.onClickCancel();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,8 +3,13 @@ package org.sufficientlysecure.keychain.remote.ui;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
||||
import org.sufficientlysecure.keychain.Constants;
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.provider.ApiDataAccessObject;
|
||||
import org.sufficientlysecure.keychain.remote.AppSettings;
|
||||
import org.sufficientlysecure.keychain.util.Log;
|
||||
@@ -12,24 +17,46 @@ import org.sufficientlysecure.keychain.util.Log;
|
||||
|
||||
class RemoteRegisterPresenter {
|
||||
private final ApiDataAccessObject apiDao;
|
||||
private final PackageManager packageManager;
|
||||
private final Context context;
|
||||
|
||||
|
||||
private RemoteRegisterView view;
|
||||
private Intent resultData;
|
||||
AppSettings appSettings;
|
||||
private AppSettings appSettings;
|
||||
|
||||
|
||||
RemoteRegisterPresenter(Context context) {
|
||||
this.context = context;
|
||||
|
||||
apiDao = new ApiDataAccessObject(context);
|
||||
packageManager = context.getPackageManager();
|
||||
}
|
||||
|
||||
public void setView(RemoteRegisterView view) {
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
void setupFromIntent(Intent resultData, String packageName, byte[] packageSignature) {
|
||||
void setupFromIntentData(Intent resultData, String packageName, byte[] packageSignature) {
|
||||
this.appSettings = new AppSettings(packageName, packageSignature);
|
||||
this.resultData = resultData;
|
||||
|
||||
Log.d(Constants.TAG, "ACTION_REGISTER packageName: " + packageName);
|
||||
try {
|
||||
setPackageInfo(packageName);
|
||||
} catch (NameNotFoundException e) {
|
||||
Log.e(Constants.TAG, "Unable to find info of calling app!");
|
||||
view.finishAsCancelled();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void setPackageInfo(String packageName) throws NameNotFoundException {
|
||||
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, 0);
|
||||
Drawable appIcon = packageManager.getApplicationIcon(applicationInfo);
|
||||
CharSequence appName = packageManager.getApplicationLabel(applicationInfo);
|
||||
|
||||
view.setTitleClientIcon(appIcon);
|
||||
view.setTitleText(context.getString(R.string.api_register_text, appName));
|
||||
}
|
||||
|
||||
void onClickAllow() {
|
||||
@@ -41,9 +68,16 @@ class RemoteRegisterPresenter {
|
||||
view.finishAsCancelled();
|
||||
}
|
||||
|
||||
void onCancel() {
|
||||
view.finishAsCancelled();
|
||||
}
|
||||
|
||||
interface RemoteRegisterView {
|
||||
void finishWithResult(Intent resultData);
|
||||
void finishAsCancelled();
|
||||
|
||||
void setTitleText(String text);
|
||||
void setTitleClientIcon(Drawable drawable);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user