Prefer AppCompat classes

AppCompat is the preferred way to go for any app targetting
a wider range of SDKs.

Replace all activities and fragments with their AppCompat
variants and fixup method calls to use support variants.

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2018-04-27 22:29:27 +05:30
parent af57824c14
commit 4ec0b3a908
8 changed files with 44 additions and 44 deletions

View File

@ -43,6 +43,8 @@ dependencies {
implementation "com.google.dagger:dagger:$daggerVersion" implementation "com.google.dagger:dagger:$daggerVersion"
implementation "com.android.databinding:library:$databindingVersion" implementation "com.android.databinding:library:$databindingVersion"
implementation "com.android.support:support-annotations:$supportLibsVersion" implementation "com.android.support:support-annotations:$supportLibsVersion"
implementation "com.android.support:appcompat-v7:$supportLibsVersion"
implementation "com.android.support:preference-v7:$supportLibsVersion"
implementation "com.commonsware.cwac:crossport:$crossportVersion" implementation "com.commonsware.cwac:crossport:$crossportVersion"
implementation "com.getbase:floatingactionbutton:$fabLibVersion" implementation "com.getbase:floatingactionbutton:$fabLibVersion"
implementation "net.sourceforge.streamsupport:android-retrofuture:$streamsupportVersion" implementation "net.sourceforge.streamsupport:android-retrofuture:$streamsupportVersion"

View File

@ -1,10 +1,10 @@
package com.wireguard.android.activity; package com.wireguard.android.activity;
import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.databinding.CallbackRegistry; import android.databinding.CallbackRegistry;
import android.databinding.CallbackRegistry.NotifierCallback; import android.databinding.CallbackRegistry.NotifierCallback;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.wireguard.android.Application; import com.wireguard.android.Application;
import com.wireguard.android.backend.GoBackend; import com.wireguard.android.backend.GoBackend;
@ -17,7 +17,7 @@ import java.util.Objects;
* Base class for activities that need to remember the currently-selected tunnel. * Base class for activities that need to remember the currently-selected tunnel.
*/ */
public abstract class BaseActivity extends Activity { public abstract class BaseActivity extends AppCompatActivity {
private static final String KEY_SELECTED_TUNNEL = "selected_tunnel"; private static final String KEY_SELECTED_TUNNEL = "selected_tunnel";
private final SelectionChangeRegistry selectionChangeRegistry = new SelectionChangeRegistry(); private final SelectionChangeRegistry selectionChangeRegistry = new SelectionChangeRegistry();

View File

@ -1,9 +1,10 @@
package com.wireguard.android.activity; package com.wireguard.android.activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log; import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
@ -28,6 +29,7 @@ public class MainActivity extends BaseActivity {
private State state = State.EMPTY; private State state = State.EMPTY;
private boolean moveToState(final State nextState) { private boolean moveToState(final State nextState) {
final FragmentManager fragmentManager = getSupportFragmentManager();
Log.i(TAG, "Moving from " + state.name() + " to " + nextState.name()); Log.i(TAG, "Moving from " + state.name() + " to " + nextState.name());
if (nextState == state) { if (nextState == state) {
return false; return false;
@ -37,16 +39,16 @@ public class MainActivity extends BaseActivity {
return true; return true;
} else if (nextState.layer == state.layer + 1) { } else if (nextState.layer == state.layer + 1) {
final Fragment fragment = Fragment.instantiate(this, nextState.fragment); final Fragment fragment = Fragment.instantiate(this, nextState.fragment);
final FragmentTransaction transaction = getFragmentManager().beginTransaction() final FragmentTransaction transaction = fragmentManager.beginTransaction()
.replace(R.id.master_fragment, fragment) .replace(R.id.master_fragment, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
if (state.layer > 0) if (state.layer > 0)
transaction.addToBackStack(null); transaction.addToBackStack(null);
transaction.commit(); transaction.commit();
} else if (nextState.layer == state.layer - 1) { } else if (nextState.layer == state.layer - 1) {
if (getFragmentManager().getBackStackEntryCount() == 0) if (fragmentManager.getBackStackEntryCount() == 0)
return false; return false;
getFragmentManager().popBackStack(); fragmentManager.popBackStack();
} else if (nextState.layer < state.layer - 1) { } else if (nextState.layer < state.layer - 1) {
moveToState(State.ofLayer(state.layer - 1)); moveToState(State.ofLayer(state.layer - 1));
moveToState(nextState); moveToState(nextState);
@ -121,8 +123,8 @@ public class MainActivity extends BaseActivity {
} }
private void updateActionBar() { private void updateActionBar() {
if (getActionBar() != null) if (getSupportActionBar() != null)
getActionBar().setDisplayHomeAsUpEnabled(state.layer > State.LIST.layer); getSupportActionBar().setDisplayHomeAsUpEnabled(state.layer > State.LIST.layer);
} }
private enum State { private enum State {

View File

@ -1,10 +1,11 @@
package com.wireguard.android.activity; package com.wireguard.android.activity;
import android.app.Activity;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Bundle; import android.os.Bundle;
import android.preference.Preference; import android.support.annotation.NonNull;
import android.preference.PreferenceFragment; import android.support.v7.app.AppCompatActivity;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import com.wireguard.android.Application; import com.wireguard.android.Application;
import com.wireguard.android.R; import com.wireguard.android.R;
@ -19,7 +20,7 @@ import java.util.List;
* Interface for changing application-global persistent settings. * Interface for changing application-global persistent settings.
*/ */
public class SettingsActivity extends Activity { public class SettingsActivity extends AppCompatActivity {
@FunctionalInterface @FunctionalInterface
public interface PermissionRequestCallback { public interface PermissionRequestCallback {
void done(String[] permissions, int[] grantResults); void done(String[] permissions, int[] grantResults);
@ -56,7 +57,7 @@ public class SettingsActivity extends Activity {
} }
@Override @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
final PermissionRequestCallback f = permissionRequestCallbacks.get(requestCode); final PermissionRequestCallback f = permissionRequestCallbacks.get(requestCode);
if (f != null) { if (f != null) {
permissionRequestCallbacks.remove(requestCode); permissionRequestCallbacks.remove(requestCode);
@ -67,17 +68,16 @@ public class SettingsActivity extends Activity {
@Override @Override
protected void onCreate(final Bundle savedInstanceState) { protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (getFragmentManager().findFragmentById(android.R.id.content) == null) { if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
getFragmentManager().beginTransaction() getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, new SettingsFragment()) .add(android.R.id.content, new SettingsFragment())
.commit(); .commit();
} }
} }
public static class SettingsFragment extends PreferenceFragment { public static class SettingsFragment extends PreferenceFragmentCompat {
@Override @Override
public void onCreate(final Bundle savedInstanceState) { public void onCreatePreferences(final Bundle savedInstanceState, final String key) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences); addPreferencesFromResource(R.xml.preferences);
if (Application.getComponent().getBackendType() != WgQuickBackend.class) { if (Application.getComponent().getBackendType() != WgQuickBackend.class) {
final Preference toolsInstaller = final Preference toolsInstaller =

View File

@ -14,8 +14,8 @@ public class TunnelCreatorActivity extends BaseActivity {
@SuppressWarnings("UnnecessaryFullyQualifiedName") @SuppressWarnings("UnnecessaryFullyQualifiedName")
protected void onCreate(final Bundle savedInstanceState) { protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (getFragmentManager().findFragmentById(android.R.id.content) == null) { if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
getFragmentManager().beginTransaction() getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, new TunnelEditorFragment()) .add(android.R.id.content, new TunnelEditorFragment())
.commit(); .commit();
} }

View File

@ -1,7 +1,7 @@
package com.wireguard.android.fragment; package com.wireguard.android.fragment;
import android.app.Fragment;
import android.content.Context; import android.content.Context;
import android.support.v4.app.Fragment;
import com.wireguard.android.activity.BaseActivity; import com.wireguard.android.activity.BaseActivity;
import com.wireguard.android.activity.BaseActivity.OnSelectedTunnelChangedListener; import com.wireguard.android.activity.BaseActivity.OnSelectedTunnelChangedListener;

View File

@ -1,8 +1,8 @@
package com.wireguard.android.preference; package com.wireguard.android.preference;
import android.content.Context; import android.content.Context;
import android.preference.Preference;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v7.preference.Preference;
import android.system.OsConstants; import android.system.OsConstants;
import android.util.AttributeSet; import android.util.AttributeSet;
@ -37,17 +37,12 @@ public class ToolsInstallerPreference extends Preference {
@Override @Override
public CharSequence getTitle() { public CharSequence getTitle() {
return getContext().getString(getTitleRes()); return getContext().getString(R.string.tools_installer_title);
} }
@Override @Override
public int getTitleRes() { public void onAttached() {
return R.string.tools_installer_title; super.onAttached();
}
@Override
protected void onAttachedToActivity() {
super.onAttachedToActivity();
asyncWorker.supplyAsync(toolsInstaller::areInstalled).whenComplete(this::onCheckResult); asyncWorker.supplyAsync(toolsInstaller::areInstalled).whenComplete(this::onCheckResult);
} }

View File

@ -1,9 +1,10 @@
package com.wireguard.android.preference; package com.wireguard.android.preference;
import android.Manifest;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Environment; import android.os.Environment;
import android.preference.Preference; import android.support.v7.preference.Preference;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log; import android.util.Log;
@ -57,12 +58,7 @@ public class ZipExporterPreference extends Preference {
@Override @Override
public CharSequence getTitle() { public CharSequence getTitle() {
return getContext().getString(getTitleRes()); return getContext().getString(R.string.zip_exporter_title);
}
@Override
public int getTitleRes() {
return R.string.zip_exporter_title;
} }
private void exportZip() { private void exportZip() {
@ -86,7 +82,8 @@ public class ZipExporterPreference extends Preference {
final ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(file)); final ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(file));
for (int i = 0; i < futureConfigs.size(); ++i) { for (int i = 0; i < futureConfigs.size(); ++i) {
zip.putNextEntry(new ZipEntry(tunnels.get(i).getName() + ".conf")); zip.putNextEntry(new ZipEntry(tunnels.get(i).getName() + ".conf"));
zip.write(futureConfigs.get(i).getNow(null).toString().getBytes(StandardCharsets.UTF_8)); zip.write(futureConfigs.get(i).getNow(null).
toString().getBytes(StandardCharsets.UTF_8));
} }
zip.closeEntry(); zip.closeEntry();
zip.close(); zip.close();
@ -104,7 +101,9 @@ public class ZipExporterPreference extends Preference {
final String error = ExceptionLoggers.unwrap(throwable).getMessage(); final String error = ExceptionLoggers.unwrap(throwable).getMessage();
final String message = getContext().getString(R.string.export_error, error); final String message = getContext().getString(R.string.export_error, error);
Log.e(TAG, message, throwable); Log.e(TAG, message, throwable);
Snackbar.make(((SettingsActivity)getContext()).findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG).show(); Snackbar.make(
((SettingsActivity)getContext()).findViewById(android.R.id.content),
message, Snackbar.LENGTH_LONG).show();
} else { } else {
exportedFilePath = filePath; exportedFilePath = filePath;
setEnabled(false); setEnabled(false);
@ -114,10 +113,12 @@ public class ZipExporterPreference extends Preference {
@Override @Override
protected void onClick() { protected void onClick() {
((SettingsActivity)getContext()).ensurePermissions(new String[] { "android.permission.WRITE_EXTERNAL_STORAGE" }, (permissions, granted) -> { ((SettingsActivity)getContext()).ensurePermissions(
if (granted.length > 0 && granted[0] == PackageManager.PERMISSION_GRANTED) new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
exportZip(); (permissions, granted) -> {
}); if (granted.length > 0 && granted[0] == PackageManager.PERMISSION_GRANTED)
exportZip();
});
} }
} }