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.android.databinding:library:$databindingVersion"
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.getbase:floatingactionbutton:$fabLibVersion"
implementation "net.sourceforge.streamsupport:android-retrofuture:$streamsupportVersion"

View File

@ -1,10 +1,10 @@
package com.wireguard.android.activity;
import android.app.Activity;
import android.content.Intent;
import android.databinding.CallbackRegistry;
import android.databinding.CallbackRegistry.NotifierCallback;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.wireguard.android.Application;
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.
*/
public abstract class BaseActivity extends Activity {
public abstract class BaseActivity extends AppCompatActivity {
private static final String KEY_SELECTED_TUNNEL = "selected_tunnel";
private final SelectionChangeRegistry selectionChangeRegistry = new SelectionChangeRegistry();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,9 +1,10 @@
package com.wireguard.android.preference;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.preference.Preference;
import android.support.v7.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
@ -57,12 +58,7 @@ public class ZipExporterPreference extends Preference {
@Override
public CharSequence getTitle() {
return getContext().getString(getTitleRes());
}
@Override
public int getTitleRes() {
return R.string.zip_exporter_title;
return getContext().getString(R.string.zip_exporter_title);
}
private void exportZip() {
@ -86,7 +82,8 @@ public class ZipExporterPreference extends Preference {
final ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(file));
for (int i = 0; i < futureConfigs.size(); ++i) {
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.close();
@ -104,7 +101,9 @@ public class ZipExporterPreference extends Preference {
final String error = ExceptionLoggers.unwrap(throwable).getMessage();
final String message = getContext().getString(R.string.export_error, error);
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 {
exportedFilePath = filePath;
setEnabled(false);
@ -114,7 +113,9 @@ public class ZipExporterPreference extends Preference {
@Override
protected void onClick() {
((SettingsActivity)getContext()).ensurePermissions(new String[] { "android.permission.WRITE_EXTERNAL_STORAGE" }, (permissions, granted) -> {
((SettingsActivity)getContext()).ensurePermissions(
new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
(permissions, granted) -> {
if (granted.length > 0 && granted[0] == PackageManager.PERMISSION_GRANTED)
exportZip();
});