Merge pull request #81 from Maxr1998/splash-and-login-improvements
Refactor splash and login code
This commit is contained in:
commit
6faab1db2b
2 changed files with 33 additions and 36 deletions
|
|
@ -47,6 +47,7 @@ public class LoginActivity extends AbsBaseActivity implements View.OnClickListen
|
|||
private void setUpToolbar() {
|
||||
binding.toolbar.setBackgroundColor(ThemeStore.primaryColor(this));
|
||||
setSupportActionBar(binding.toolbar);
|
||||
// noinspection ConstantConditions
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
}
|
||||
|
||||
|
|
@ -83,7 +84,7 @@ public class LoginActivity extends AbsBaseActivity implements View.OnClickListen
|
|||
@Override
|
||||
public void onResponse(AuthenticationResult result) {
|
||||
if (result.getAccessToken() == null) return;
|
||||
check(context, binding.server.getText().toString(), result.getUser().getId(), result.getAccessToken());
|
||||
check(binding.server.getText().toString(), result.getUser().getId(), result.getAccessToken());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -94,20 +95,21 @@ public class LoginActivity extends AbsBaseActivity implements View.OnClickListen
|
|||
}
|
||||
}
|
||||
|
||||
public void check(Context context, String server, String user, String token) {
|
||||
private void check(String server, String user, String token) {
|
||||
App.getApiClient().GetSystemInfoAsync(new Response<SystemInfo>() {
|
||||
@Override
|
||||
public void onResponse(SystemInfo result) {
|
||||
if (Integer.parseInt(result.getVersion().substring(0, 1)) == 1) {
|
||||
PreferenceUtil.getInstance(context).setServer(server);
|
||||
PreferenceUtil.getInstance(context).setUser(user);
|
||||
PreferenceUtil.getInstance(context).setToken(token);
|
||||
if (result.getVersion().charAt(0) == '1') {
|
||||
PreferenceUtil.getInstance(LoginActivity.this).setServer(server);
|
||||
PreferenceUtil.getInstance(LoginActivity.this).setUser(user);
|
||||
PreferenceUtil.getInstance(LoginActivity.this).setToken(token);
|
||||
|
||||
Intent intent = new Intent(context, MainActivity.class);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||
context.startActivity(intent);
|
||||
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
||||
startActivity(intent);
|
||||
finish();
|
||||
} else {
|
||||
Toast.makeText(context, context.getResources().getString(R.string.error_version), Toast.LENGTH_SHORT).show();
|
||||
Toast.makeText(LoginActivity.this, getResources().getString(R.string.error_version), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -2,13 +2,14 @@ package com.dkanada.gramophone.ui.activities;
|
|||
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.PowerManager;
|
||||
import android.provider.Settings;
|
||||
|
||||
import androidx.annotation.RequiresApi;
|
||||
|
||||
import com.dkanada.gramophone.App;
|
||||
import com.dkanada.gramophone.R;
|
||||
import com.dkanada.gramophone.ui.activities.base.AbsBaseActivity;
|
||||
|
|
@ -32,40 +33,31 @@ public class SplashActivity extends AbsBaseActivity {
|
|||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
if (detectBatteryOptimization()) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && detectBatteryOptimization()) {
|
||||
showBatteryOptimizationDialog();
|
||||
} else {
|
||||
login();
|
||||
}
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||
private boolean detectBatteryOptimization() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
String packageName = getPackageName();
|
||||
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
|
||||
return !pm.isIgnoringBatteryOptimizations(packageName);
|
||||
}
|
||||
|
||||
return false;
|
||||
String packageName = getPackageName();
|
||||
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
|
||||
return !pm.isIgnoringBatteryOptimizations(packageName);
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||
private void showBatteryOptimizationDialog() {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(SplashActivity.this);
|
||||
builder.setMessage(R.string.battery_optimizations_message)
|
||||
.setTitle(R.string.battery_optimizations_title)
|
||||
.setNegativeButton(R.string.ignore, new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
login();
|
||||
}
|
||||
})
|
||||
.setPositiveButton(R.string.disable, new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
openPowerSettings(SplashActivity.this);
|
||||
}
|
||||
})
|
||||
.setNegativeButton(R.string.ignore, (dialog, id) -> login())
|
||||
.setPositiveButton(R.string.disable, (dialog, id) -> openPowerSettings(SplashActivity.this))
|
||||
.show();
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||
private void openPowerSettings(Context context) {
|
||||
Intent intent = new Intent();
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
|
|
@ -75,9 +67,7 @@ public class SplashActivity extends AbsBaseActivity {
|
|||
|
||||
public void login() {
|
||||
if (PreferenceUtil.getInstance(this).getToken() == null) {
|
||||
Intent intent = new Intent(this, LoginActivity.class);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||
startActivity(intent);
|
||||
launchLoginActivity();
|
||||
} else {
|
||||
final Context context = this;
|
||||
|
||||
|
|
@ -93,11 +83,16 @@ public class SplashActivity extends AbsBaseActivity {
|
|||
|
||||
@Override
|
||||
public void onError(Exception exception) {
|
||||
Intent intent = new Intent(context, LoginActivity.class);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||
startActivity(intent);
|
||||
launchLoginActivity();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void launchLoginActivity() {
|
||||
Intent intent = new Intent(this, LoginActivity.class);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
||||
startActivity(intent);
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue