2017-02-16 19:52:55 +01:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
2017-04-10 02:12:31 +02:00
|
|
|
* This file is a part of Tusky.
|
2017-02-16 19:52:55 +01:00
|
|
|
*
|
2017-04-10 02:12:31 +02:00
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2017-02-16 19:52:55 +01:00
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
2017-04-10 02:12:31 +02:00
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
2017-02-16 19:52:55 +01:00
|
|
|
*
|
2017-04-10 02:12:31 +02:00
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
2017-02-16 19:52:55 +01:00
|
|
|
|
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
2018-12-17 15:25:35 +01:00
|
|
|
import android.app.ActivityManager;
|
2019-03-08 20:33:42 +01:00
|
|
|
import android.content.Context;
|
2017-03-07 13:05:51 +01:00
|
|
|
import android.content.Intent;
|
2017-03-08 22:34:13 +01:00
|
|
|
import android.content.SharedPreferences;
|
2019-03-23 12:49:36 +01:00
|
|
|
import android.content.pm.PackageManager;
|
2018-12-17 15:25:35 +01:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
2017-02-16 19:52:55 +01:00
|
|
|
import android.os.Bundle;
|
2018-07-30 15:31:35 +02:00
|
|
|
import android.util.Log;
|
2018-10-01 11:50:17 +02:00
|
|
|
import android.view.View;
|
2017-02-16 19:52:55 +01:00
|
|
|
|
2019-05-26 08:12:42 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.annotation.StringRes;
|
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
import androidx.core.app.ActivityCompat;
|
|
|
|
import androidx.core.content.ContextCompat;
|
2019-10-22 21:18:20 +02:00
|
|
|
import androidx.preference.PreferenceManager;
|
2019-05-26 08:12:42 +02:00
|
|
|
|
2018-12-17 15:25:35 +01:00
|
|
|
import com.google.android.material.snackbar.Snackbar;
|
2019-02-06 10:23:02 +01:00
|
|
|
import com.keylesspalace.tusky.adapter.AccountSelectionAdapter;
|
2018-02-03 22:45:14 +01:00
|
|
|
import com.keylesspalace.tusky.db.AccountEntity;
|
2018-03-09 22:02:32 +01:00
|
|
|
import com.keylesspalace.tusky.db.AccountManager;
|
2018-05-08 19:15:10 +02:00
|
|
|
import com.keylesspalace.tusky.di.Injectable;
|
2019-02-06 10:23:02 +01:00
|
|
|
import com.keylesspalace.tusky.interfaces.AccountSelectionListener;
|
2019-03-23 12:49:36 +01:00
|
|
|
import com.keylesspalace.tusky.interfaces.PermissionRequester;
|
2018-01-20 13:39:01 +01:00
|
|
|
import com.keylesspalace.tusky.util.ThemeUtils;
|
2017-03-08 23:19:03 +01:00
|
|
|
|
2018-05-06 22:05:54 +02:00
|
|
|
import java.util.ArrayList;
|
2019-03-23 12:49:36 +01:00
|
|
|
import java.util.HashMap;
|
2018-05-06 22:05:54 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2018-05-06 11:07:10 +02:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2018-05-08 19:15:10 +02:00
|
|
|
public abstract class BaseActivity extends AppCompatActivity implements Injectable {
|
2017-03-20 03:38:39 +01:00
|
|
|
|
2018-05-06 11:07:10 +02:00
|
|
|
@Inject
|
|
|
|
public AccountManager accountManager;
|
|
|
|
|
2019-03-23 12:49:36 +01:00
|
|
|
private static final int REQUESTER_NONE = Integer.MAX_VALUE;
|
|
|
|
private HashMap<Integer, PermissionRequester> requesters;
|
2018-10-01 11:50:17 +02:00
|
|
|
|
2017-02-16 19:52:55 +01:00
|
|
|
@Override
|
|
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2017-03-09 00:27:37 +01:00
|
|
|
|
2017-12-01 21:52:10 +01:00
|
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
|
2017-04-28 05:29:42 +02:00
|
|
|
/* There isn't presently a way to globally change the theme of a whole application at
|
|
|
|
* runtime, just individual activities. So, each activity has to set its theme before any
|
|
|
|
* views are created. */
|
2018-04-22 17:20:01 +02:00
|
|
|
String theme = preferences.getString("appTheme", ThemeUtils.APP_THEME_DEFAULT);
|
2018-07-30 15:31:35 +02:00
|
|
|
Log.d("activeTheme", theme);
|
|
|
|
if (theme.equals("black")) {
|
|
|
|
setTheme(R.style.TuskyBlackTheme);
|
|
|
|
}
|
2019-03-07 21:33:29 +01:00
|
|
|
|
2018-12-17 15:25:35 +01:00
|
|
|
/* set the taskdescription programmatically, the theme would turn it blue */
|
|
|
|
String appName = getString(R.string.app_name);
|
|
|
|
Bitmap appIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
|
2020-01-30 21:37:28 +01:00
|
|
|
int recentsBackgroundColor = ThemeUtils.getColor(this, R.attr.colorSurface);
|
2018-12-17 15:25:35 +01:00
|
|
|
|
|
|
|
setTaskDescription(new ActivityManager.TaskDescription(appName, appIcon, recentsBackgroundColor));
|
|
|
|
|
2018-06-07 22:19:39 +02:00
|
|
|
int style = textStyle(preferences.getString("statusTextSize", "medium"));
|
|
|
|
getTheme().applyStyle(style, false);
|
|
|
|
|
2018-12-17 15:25:35 +01:00
|
|
|
if(requiresLogin()) {
|
|
|
|
redirectIfNotLoggedIn();
|
|
|
|
}
|
2018-06-07 22:19:39 +02:00
|
|
|
|
2019-03-23 12:49:36 +01:00
|
|
|
requesters = new HashMap<>();
|
2018-06-07 22:19:39 +02:00
|
|
|
}
|
|
|
|
|
2019-03-08 20:33:42 +01:00
|
|
|
@Override
|
|
|
|
protected void attachBaseContext(Context base) {
|
2020-02-25 19:49:15 +01:00
|
|
|
super.attachBaseContext(TuskyApplication.getLocaleManager().setLocale(base));
|
2019-03-08 20:33:42 +01:00
|
|
|
}
|
|
|
|
|
2018-12-17 15:25:35 +01:00
|
|
|
protected boolean requiresLogin() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-20 22:36:44 +02:00
|
|
|
private static int textStyle(String name) {
|
2017-12-01 21:52:10 +01:00
|
|
|
int style;
|
2018-06-07 22:19:39 +02:00
|
|
|
switch (name) {
|
|
|
|
case "smallest":
|
|
|
|
style = R.style.TextSizeSmallest;
|
2017-12-01 21:52:10 +01:00
|
|
|
break;
|
|
|
|
case "small":
|
|
|
|
style = R.style.TextSizeSmall;
|
|
|
|
break;
|
2017-12-01 22:42:43 +01:00
|
|
|
case "medium":
|
|
|
|
default:
|
|
|
|
style = R.style.TextSizeMedium;
|
|
|
|
break;
|
2018-06-07 22:19:39 +02:00
|
|
|
case "large":
|
|
|
|
style = R.style.TextSizeLarge;
|
|
|
|
break;
|
|
|
|
case "largest":
|
|
|
|
style = R.style.TextSizeLargest;
|
|
|
|
break;
|
2017-12-01 21:52:10 +01:00
|
|
|
}
|
2018-06-07 22:19:39 +02:00
|
|
|
return style;
|
2017-03-15 01:34:27 +01:00
|
|
|
}
|
|
|
|
|
2018-07-31 21:25:25 +02:00
|
|
|
public void startActivityWithSlideInAnimation(Intent intent) {
|
2017-03-07 13:05:51 +01:00
|
|
|
super.startActivity(intent);
|
|
|
|
overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left);
|
|
|
|
}
|
|
|
|
|
2018-08-07 23:08:53 +02:00
|
|
|
@Override
|
|
|
|
public void finish() {
|
2018-07-31 21:25:25 +02:00
|
|
|
super.finish();
|
2017-03-07 13:05:51 +01:00
|
|
|
overridePendingTransition(R.anim.slide_from_left, R.anim.slide_to_right);
|
|
|
|
}
|
|
|
|
|
2018-08-07 23:08:53 +02:00
|
|
|
public void finishWithoutSlideOutAnimation() {
|
|
|
|
super.finish();
|
|
|
|
}
|
|
|
|
|
2018-05-08 19:15:10 +02:00
|
|
|
protected void redirectIfNotLoggedIn() {
|
|
|
|
AccountEntity account = accountManager.getActiveAccount();
|
2018-03-27 19:47:00 +02:00
|
|
|
if (account == null) {
|
2017-04-05 22:35:22 +02:00
|
|
|
Intent intent = new Intent(this, LoginActivity.class);
|
2017-04-05 21:10:52 +02:00
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
|
2018-07-31 21:25:25 +02:00
|
|
|
startActivityWithSlideInAnimation(intent);
|
2018-08-07 23:08:53 +02:00
|
|
|
finish();
|
2017-04-05 21:10:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 11:50:17 +02:00
|
|
|
protected void showErrorDialog(View anyView, @StringRes int descriptionId, @StringRes int actionId, View.OnClickListener listener) {
|
|
|
|
if (anyView != null) {
|
|
|
|
Snackbar bar = Snackbar.make(anyView, getString(descriptionId), Snackbar.LENGTH_SHORT);
|
|
|
|
bar.setAction(actionId, listener);
|
|
|
|
bar.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-06 10:23:02 +01:00
|
|
|
public void showAccountChooserDialog(CharSequence dialogTitle, boolean showActiveAccount, AccountSelectionListener listener) {
|
|
|
|
List<AccountEntity> accounts = accountManager.getAllAccountsOrderedByActive();
|
|
|
|
AccountEntity activeAccount = accountManager.getActiveAccount();
|
|
|
|
|
|
|
|
switch(accounts.size()) {
|
|
|
|
case 1:
|
|
|
|
listener.onAccountSelected(activeAccount);
|
|
|
|
return;
|
|
|
|
case 2:
|
|
|
|
if (!showActiveAccount) {
|
|
|
|
for (AccountEntity account : accounts) {
|
|
|
|
if (activeAccount != account) {
|
|
|
|
listener.onAccountSelected(account);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!showActiveAccount && activeAccount != null) {
|
|
|
|
accounts.remove(activeAccount);
|
|
|
|
}
|
|
|
|
AccountSelectionAdapter adapter = new AccountSelectionAdapter(this);
|
|
|
|
adapter.addAll(accounts);
|
|
|
|
|
|
|
|
new AlertDialog.Builder(this)
|
2019-04-21 22:59:49 +02:00
|
|
|
.setTitle(dialogTitle)
|
|
|
|
.setAdapter(adapter, (dialogInterface, index) -> listener.onAccountSelected(accounts.get(index)))
|
|
|
|
.show();
|
2019-02-06 10:23:02 +01:00
|
|
|
}
|
2019-03-23 12:49:36 +01:00
|
|
|
|
|
|
|
@Override
|
2019-04-21 22:59:49 +02:00
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
2019-03-23 12:49:36 +01:00
|
|
|
if (requesters.containsKey(requestCode)) {
|
|
|
|
PermissionRequester requester = requesters.remove(requestCode);
|
|
|
|
requester.onRequestPermissionsResult(permissions, grantResults);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-21 22:59:49 +02:00
|
|
|
public void requestPermissions(String[] permissions, PermissionRequester requester) {
|
|
|
|
ArrayList<String> permissionsToRequest = new ArrayList<>();
|
|
|
|
for(String permission: permissions) {
|
|
|
|
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
permissionsToRequest.add(permission);
|
2019-03-23 12:49:36 +01:00
|
|
|
}
|
2019-04-21 22:59:49 +02:00
|
|
|
}
|
|
|
|
if (permissionsToRequest.isEmpty()) {
|
|
|
|
int[] permissionsAlreadyGranted = new int[permissions.length];
|
|
|
|
for (int i = 0; i < permissionsAlreadyGranted.length; ++i)
|
|
|
|
permissionsAlreadyGranted[i] = PackageManager.PERMISSION_GRANTED;
|
|
|
|
requester.onRequestPermissionsResult(permissions, permissionsAlreadyGranted);
|
|
|
|
return;
|
|
|
|
}
|
2019-03-23 12:49:36 +01:00
|
|
|
|
2019-04-21 22:59:49 +02:00
|
|
|
int newKey = requester == null ? REQUESTER_NONE : requesters.size();
|
|
|
|
if (newKey != REQUESTER_NONE) {
|
|
|
|
requesters.put(newKey, requester);
|
2019-03-23 12:49:36 +01:00
|
|
|
}
|
2019-04-21 22:59:49 +02:00
|
|
|
String[] permissionsCopy = new String[permissionsToRequest.size()];
|
|
|
|
permissionsToRequest.toArray(permissionsCopy);
|
|
|
|
ActivityCompat.requestPermissions(this, permissionsCopy, newKey);
|
|
|
|
|
2019-03-23 12:49:36 +01:00
|
|
|
}
|
2017-02-16 19:52:55 +01:00
|
|
|
}
|