ProfileActivity: Extract base class for fixing fragments
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
529e320e3f
commit
2e3daa8913
@ -12,7 +12,7 @@
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@android:style/Theme.Material.Light.DarkActionBar">
|
||||
<activity android:name=".ProfileActivity">
|
||||
<activity android:name=".ProfileListActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
|
@ -1,38 +1,26 @@
|
||||
package com.wireguard.android;
|
||||
|
||||
import android.app.Fragment;
|
||||
import android.app.FragmentManager;
|
||||
import android.app.FragmentTransaction;
|
||||
import android.content.res.Configuration;
|
||||
import android.os.Bundle;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
|
||||
/**
|
||||
* Activity that allows creating/viewing/editing/deleting WireGuard profiles.
|
||||
* Base class for activities that use ProfileListFragment and ProfileDetailFragment.
|
||||
*/
|
||||
|
||||
public class ProfileActivity extends ServiceClientActivity<ProfileServiceInterface> {
|
||||
class ProfileActivity extends ServiceClientActivity<ProfileServiceInterface> {
|
||||
public static final String KEY_PROFILE_NAME = "profile_name";
|
||||
|
||||
// FIXME: These must match the constants in profile_list_activity.xml
|
||||
private static final String TAG_DETAIL = "detail";
|
||||
private static final String TAG_LIST = "list";
|
||||
protected static final String TAG_DETAIL = "detail";
|
||||
protected static final String TAG_LIST = "list";
|
||||
|
||||
private String currentProfile;
|
||||
private boolean isSplitLayout;
|
||||
|
||||
public ProfileActivity() {
|
||||
super(ProfileService.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
if (!isSplitLayout && currentProfile != null) {
|
||||
onProfileSelected(null);
|
||||
} else {
|
||||
super.onBackPressed();
|
||||
}
|
||||
protected String getCurrentProfile() {
|
||||
return currentProfile;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -43,11 +31,6 @@ public class ProfileActivity extends ServiceClientActivity<ProfileServiceInterfa
|
||||
currentProfile = savedInstanceState.getString(KEY_PROFILE_NAME);
|
||||
else
|
||||
currentProfile = getIntent().getStringExtra(KEY_PROFILE_NAME);
|
||||
// Set up the base layout and fill it with fragments.
|
||||
setContentView(R.layout.profile_activity);
|
||||
final int orientation = getResources().getConfiguration().orientation;
|
||||
isSplitLayout = orientation == Configuration.ORIENTATION_LANDSCAPE;
|
||||
updateLayout(currentProfile);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -68,42 +51,13 @@ public class ProfileActivity extends ServiceClientActivity<ProfileServiceInterfa
|
||||
|
||||
}
|
||||
|
||||
public void onProfileSelected(String profile) {
|
||||
updateLayout(profile);
|
||||
currentProfile = profile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
outState.putString(KEY_PROFILE_NAME, currentProfile);
|
||||
}
|
||||
|
||||
private void updateLayout(String profile) {
|
||||
final FragmentManager fm = getFragmentManager();
|
||||
final Fragment detailFragment = fm.findFragmentByTag(TAG_DETAIL);
|
||||
final Fragment listFragment = fm.findFragmentByTag(TAG_LIST);
|
||||
final FragmentTransaction transaction = fm.beginTransaction();
|
||||
if (profile != null) {
|
||||
if (isSplitLayout) {
|
||||
if (listFragment.isHidden())
|
||||
transaction.show(listFragment);
|
||||
} else {
|
||||
transaction.hide(listFragment);
|
||||
}
|
||||
if (detailFragment.isHidden())
|
||||
transaction.show(detailFragment);
|
||||
} else {
|
||||
if (isSplitLayout) {
|
||||
if (detailFragment.isHidden())
|
||||
transaction.show(detailFragment);
|
||||
} else {
|
||||
transaction.hide(detailFragment);
|
||||
}
|
||||
if (listFragment.isHidden())
|
||||
transaction.show(listFragment);
|
||||
}
|
||||
transaction.commit();
|
||||
((ProfileDetailFragment) detailFragment).setProfile(profile);
|
||||
protected void setCurrentProfile(String profile) {
|
||||
currentProfile = profile;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,68 @@
|
||||
package com.wireguard.android;
|
||||
|
||||
import android.app.Fragment;
|
||||
import android.app.FragmentManager;
|
||||
import android.app.FragmentTransaction;
|
||||
import android.content.res.Configuration;
|
||||
import android.os.Bundle;
|
||||
|
||||
/**
|
||||
* Activity that allows creating/viewing/editing/deleting WireGuard profiles.
|
||||
*/
|
||||
|
||||
public class ProfileListActivity extends ProfileActivity {
|
||||
private boolean isSplitLayout;
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
final FragmentManager fm = getFragmentManager();
|
||||
if (fm.getBackStackEntryCount() > 0) {
|
||||
fm.popBackStack();
|
||||
} else {
|
||||
super.onBackPressed();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
// Set up the base layout and fill it with fragments.
|
||||
setContentView(R.layout.profile_list_activity);
|
||||
final int orientation = getResources().getConfiguration().orientation;
|
||||
isSplitLayout = orientation == Configuration.ORIENTATION_LANDSCAPE;
|
||||
updateLayout(getCurrentProfile());
|
||||
}
|
||||
|
||||
public void onProfileSelected(String profile) {
|
||||
updateLayout(profile);
|
||||
setCurrentProfile(profile);
|
||||
}
|
||||
|
||||
private void updateLayout(String profile) {
|
||||
final FragmentManager fm = getFragmentManager();
|
||||
final Fragment detailFragment = fm.findFragmentByTag(TAG_DETAIL);
|
||||
final Fragment listFragment = fm.findFragmentByTag(TAG_LIST);
|
||||
final FragmentTransaction transaction = fm.beginTransaction();
|
||||
if (profile != null) {
|
||||
if (isSplitLayout) {
|
||||
if (listFragment.isHidden())
|
||||
transaction.show(listFragment);
|
||||
} else {
|
||||
transaction.hide(listFragment);
|
||||
}
|
||||
if (detailFragment.isHidden())
|
||||
transaction.show(detailFragment);
|
||||
} else {
|
||||
if (isSplitLayout) {
|
||||
if (detailFragment.isHidden())
|
||||
transaction.show(detailFragment);
|
||||
} else {
|
||||
transaction.hide(detailFragment);
|
||||
}
|
||||
if (listFragment.isHidden())
|
||||
transaction.show(listFragment);
|
||||
}
|
||||
transaction.commit();
|
||||
((ProfileDetailFragment) detailFragment).setProfile(profile);
|
||||
}
|
||||
}
|
@ -25,7 +25,7 @@ public class ProfileListFragment extends ServiceClientFragment<ProfileServiceInt
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
final Profile profile = (Profile) parent.getItemAtPosition(position);
|
||||
((ProfileActivity) getActivity()).onProfileSelected(profile.getName());
|
||||
((ProfileListActivity) getActivity()).onProfileSelected(profile.getName());
|
||||
}
|
||||
});
|
||||
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
|
||||
|
Loading…
Reference in New Issue
Block a user