ProfileActivity: Do menus correctly

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Samuel Holland 2017-08-10 01:04:46 -05:00
parent d6d6f34088
commit 87d3200b29
7 changed files with 60 additions and 43 deletions

View File

@ -52,14 +52,6 @@ abstract class ProfileActivity extends ServiceClientActivity<ProfileServiceInter
return true;
}
public abstract void onMenuEdit(MenuItem item);
public abstract void onMenuSave(MenuItem item);
public void onMenuSettings(MenuItem item) {
startActivity(new Intent(this, SettingsActivity.class));
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

View File

@ -20,14 +20,20 @@ public class ProfileDetailActivity extends ProfileActivity {
}
@Override
public void onMenuEdit(MenuItem item) {
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_action_edit:
final Intent intent = new Intent(this, ProfileEditActivity.class);
intent.putExtra(KEY_PROFILE_NAME, getCurrentProfile());
startActivity(intent);
}
@Override
public void onMenuSave(MenuItem item) {
return true;
case R.id.menu_action_save:
throw new IllegalStateException();
case R.id.menu_settings:
startActivity(new Intent(this, SettingsActivity.class));
return true;
default:
return false;
}
}
}

View File

@ -1,5 +1,6 @@
package com.wireguard.android;
import android.content.Intent;
import android.view.MenuItem;
/**
@ -8,12 +9,18 @@ import android.view.MenuItem;
public class ProfileEditActivity extends ProfileActivity {
@Override
public void onMenuEdit(MenuItem item) {
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_action_edit:
throw new IllegalStateException();
}
@Override
public void onMenuSave(MenuItem item) {
case R.id.menu_action_save:
finish();
return false;
case R.id.menu_settings:
startActivity(new Intent(this, SettingsActivity.class));
return true;
default:
return false;
}
}
}

View File

@ -35,29 +35,28 @@ public class ProfileListActivity extends ProfileActivity {
}
transaction.commit();
if (isEditing())
onMenuEdit(null);
startEditing();
else
onProfileSelected(getCurrentProfile());
}
@Override
public void onMenuEdit(MenuItem item) {
setIsEditing(true);
if (isSplitLayout) {
updateLayout(getCurrentProfile());
} else {
final Intent intent = new Intent(this, ProfileEditActivity.class);
intent.putExtra(KEY_PROFILE_NAME, getCurrentProfile());
startActivity(intent);
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_action_edit:
startEditing();
return true;
case R.id.menu_action_save:
getFragmentManager().popBackStack();
return false;
case R.id.menu_settings:
startActivity(new Intent(this, SettingsActivity.class));
return true;
default:
return false;
}
}
@Override
public void onMenuSave(MenuItem item) {
setIsEditing(false);
}
public void onProfileSelected(String profile) {
setIsEditing(false);
if (isSplitLayout) {
@ -71,6 +70,19 @@ public class ProfileListActivity extends ProfileActivity {
}
}
private void startEditing() {
if (isSplitLayout) {
setIsEditing(true);
updateLayout(getCurrentProfile());
} else if (getCurrentProfile() != null) {
final Intent intent = new Intent(this, ProfileEditActivity.class);
intent.putExtra(KEY_PROFILE_NAME, getCurrentProfile());
startActivity(intent);
setCurrentProfile(null);
setIsEditing(false);
}
}
public void updateLayout(String profile) {
final Fragment fragment = getFragmentManager().findFragmentById(R.id.fragment_container);
if (isEditing() && profile == null)

View File

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_settings"
android:alphabeticShortcut="s"
android:icon="@drawable/ic_settings"
android:onClick="onMenuSettings"
android:showAsAction="never"
android:title="@string/settings" />
</menu>

View File

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_action_edit"
android:alphabeticShortcut="e"
android:icon="@drawable/ic_action_edit"
android:onClick="onMenuEdit"
android:showAsAction="always"
android:title="@string/edit" />
</menu>

View File

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_action_save"
android:alphabeticShortcut="s"
android:icon="@drawable/ic_action_save"
android:onClick="onMenuSave"
android:showAsAction="always"
android:title="@string/save" />
</menu>