2017-02-17 05:52:55 +11:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
2017-04-10 10:12:31 +10:00
|
|
|
* This file is a part of Tusky.
|
2017-02-17 05:52:55 +11:00
|
|
|
*
|
2017-04-10 10:12:31 +10: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-17 05:52:55 +11:00
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
2017-04-10 10:12:31 +10:00
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
2017-02-17 05:52:55 +11:00
|
|
|
*
|
2017-04-10 10:12:31 +10: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-17 05:52:55 +11:00
|
|
|
|
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
2017-03-09 08:34:13 +11:00
|
|
|
import android.content.Context;
|
2017-03-07 23:05:51 +11:00
|
|
|
import android.content.Intent;
|
2017-03-09 08:34:13 +11:00
|
|
|
import android.content.SharedPreferences;
|
2017-02-17 05:52:55 +11:00
|
|
|
import android.graphics.Color;
|
|
|
|
import android.graphics.PorterDuff;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.Bundle;
|
2017-03-20 13:38:39 +11:00
|
|
|
import android.preference.PreferenceManager;
|
2017-02-17 05:52:55 +11:00
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.v7.app.AppCompatActivity;
|
2017-03-09 09:19:03 +11:00
|
|
|
import android.text.Spanned;
|
2017-02-17 05:52:55 +11:00
|
|
|
import android.util.TypedValue;
|
|
|
|
import android.view.Menu;
|
|
|
|
|
2017-11-02 07:02:44 +11:00
|
|
|
import com.evernote.android.job.JobManager;
|
|
|
|
import com.evernote.android.job.JobRequest;
|
2017-03-09 09:19:03 +11:00
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.GsonBuilder;
|
2018-02-04 08:45:14 +11:00
|
|
|
import com.keylesspalace.tusky.db.AccountEntity;
|
2017-05-05 08:55:34 +10:00
|
|
|
import com.keylesspalace.tusky.json.SpannedTypeAdapter;
|
2017-11-02 07:02:44 +11:00
|
|
|
import com.keylesspalace.tusky.network.AuthInterceptor;
|
2017-06-23 04:01:25 +10:00
|
|
|
import com.keylesspalace.tusky.network.MastodonApi;
|
2017-05-05 08:55:34 +10:00
|
|
|
import com.keylesspalace.tusky.util.OkHttpUtils;
|
2018-01-20 23:39:01 +11:00
|
|
|
import com.keylesspalace.tusky.util.ThemeUtils;
|
2017-03-09 09:19:03 +11:00
|
|
|
|
2017-03-15 11:34:27 +11:00
|
|
|
import okhttp3.Dispatcher;
|
2017-03-09 08:34:13 +11:00
|
|
|
import okhttp3.OkHttpClient;
|
2017-10-20 00:25:04 +11:00
|
|
|
import okhttp3.logging.HttpLoggingInterceptor;
|
2017-03-09 08:34:13 +11:00
|
|
|
import retrofit2.Retrofit;
|
|
|
|
import retrofit2.converter.gson.GsonConverterFactory;
|
|
|
|
|
2017-12-02 07:52:10 +11:00
|
|
|
public abstract class BaseActivity extends AppCompatActivity {
|
2017-03-20 13:38:39 +11:00
|
|
|
|
2017-06-23 04:01:25 +10:00
|
|
|
public MastodonApi mastodonApi;
|
2017-03-15 11:34:27 +11:00
|
|
|
protected Dispatcher mastodonApiDispatcher;
|
2017-03-09 08:34:13 +11:00
|
|
|
|
2017-02-17 05:52:55 +11:00
|
|
|
@Override
|
|
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2017-03-09 10:27:37 +11:00
|
|
|
|
2017-12-02 07:52:10 +11:00
|
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
|
2017-04-28 13:29:42 +10: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-02-04 09:26:53 +11:00
|
|
|
String theme = preferences.getString("appTheme", TuskyApplication.APP_THEME_DEFAULT);
|
|
|
|
ThemeUtils.setAppNightMode(theme);
|
2017-12-02 07:52:10 +11:00
|
|
|
|
|
|
|
int style;
|
2017-12-02 08:42:43 +11:00
|
|
|
switch(preferences.getString("statusTextSize", "medium")) {
|
2017-12-02 07:52:10 +11:00
|
|
|
case "large":
|
|
|
|
style = R.style.TextSizeLarge;
|
|
|
|
break;
|
|
|
|
case "small":
|
|
|
|
style = R.style.TextSizeSmall;
|
|
|
|
break;
|
2017-12-02 08:42:43 +11:00
|
|
|
case "medium":
|
|
|
|
default:
|
|
|
|
style = R.style.TextSizeMedium;
|
|
|
|
break;
|
|
|
|
|
2017-12-02 07:52:10 +11:00
|
|
|
}
|
|
|
|
getTheme().applyStyle(style, false);
|
|
|
|
|
2018-02-05 21:27:32 +11:00
|
|
|
if(redirectIfNotLoggedIn()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
createMastodonApi();
|
|
|
|
|
2017-02-17 05:52:55 +11:00
|
|
|
}
|
|
|
|
|
2017-03-15 11:34:27 +11:00
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
2017-06-23 04:01:25 +10:00
|
|
|
if (mastodonApiDispatcher != null) {
|
|
|
|
mastodonApiDispatcher.cancelAll();
|
|
|
|
}
|
2017-03-15 11:34:27 +11:00
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
2017-03-07 23:05:51 +11:00
|
|
|
@Override
|
|
|
|
public void finish() {
|
|
|
|
super.finish();
|
|
|
|
overridePendingTransitionExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void startActivity(Intent intent) {
|
|
|
|
super.startActivity(intent);
|
|
|
|
overridePendingTransitionEnter();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void overridePendingTransitionEnter() {
|
|
|
|
overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void overridePendingTransitionExit() {
|
|
|
|
overridePendingTransition(R.anim.slide_from_left, R.anim.slide_to_right);
|
|
|
|
}
|
|
|
|
|
2017-04-21 08:56:36 +10:00
|
|
|
protected SharedPreferences getPrivatePreferences() {
|
|
|
|
return getSharedPreferences(getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
|
|
|
|
}
|
|
|
|
|
2017-03-09 08:34:13 +11:00
|
|
|
protected String getBaseUrl() {
|
2018-02-04 08:45:14 +11:00
|
|
|
AccountEntity account = TuskyApplication.getAccountManager().getActiveAccount();
|
|
|
|
if(account != null) {
|
|
|
|
return "https://" + account.getDomain();
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
2017-03-09 08:34:13 +11:00
|
|
|
}
|
|
|
|
|
2017-06-23 04:01:25 +10:00
|
|
|
protected void createMastodonApi() {
|
2017-03-15 11:34:27 +11:00
|
|
|
mastodonApiDispatcher = new Dispatcher();
|
|
|
|
|
2017-10-20 00:54:08 +11:00
|
|
|
Gson gson = new GsonBuilder()
|
|
|
|
.registerTypeAdapter(Spanned.class, new SpannedTypeAdapter())
|
2017-03-09 09:19:03 +11:00
|
|
|
.create();
|
|
|
|
|
2017-12-27 07:45:08 +11:00
|
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
|
2017-10-20 00:25:04 +11:00
|
|
|
OkHttpClient.Builder okBuilder =
|
2017-12-27 07:45:08 +11:00
|
|
|
OkHttpUtils.getCompatibleClientBuilder(preferences)
|
2018-02-04 08:45:14 +11:00
|
|
|
.addInterceptor(new AuthInterceptor())
|
2017-11-02 07:02:44 +11:00
|
|
|
.dispatcher(mastodonApiDispatcher);
|
2017-10-20 00:25:04 +11:00
|
|
|
|
|
|
|
if (BuildConfig.DEBUG) {
|
|
|
|
okBuilder.addInterceptor(
|
2017-10-30 08:18:45 +11:00
|
|
|
new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC));
|
2017-10-20 00:25:04 +11:00
|
|
|
}
|
2017-03-09 08:34:13 +11:00
|
|
|
|
2017-10-20 00:25:04 +11:00
|
|
|
Retrofit retrofit = new Retrofit.Builder().baseUrl(getBaseUrl())
|
|
|
|
.client(okBuilder.build())
|
2017-03-09 09:19:03 +11:00
|
|
|
.addConverterFactory(GsonConverterFactory.create(gson))
|
2017-03-09 08:34:13 +11:00
|
|
|
.build();
|
|
|
|
|
2017-06-23 04:01:25 +10:00
|
|
|
mastodonApi = retrofit.create(MastodonApi.class);
|
2017-03-09 08:34:13 +11:00
|
|
|
}
|
|
|
|
|
2018-02-05 21:27:32 +11:00
|
|
|
protected boolean redirectIfNotLoggedIn() {
|
2018-02-04 08:45:14 +11:00
|
|
|
if (TuskyApplication.getAccountManager().getActiveAccount() == null) {
|
2017-04-06 06:35:22 +10:00
|
|
|
Intent intent = new Intent(this, LoginActivity.class);
|
2017-04-06 05:10:52 +10:00
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
startActivity(intent);
|
|
|
|
finish();
|
2018-02-05 21:27:32 +11:00
|
|
|
return true;
|
2017-04-06 05:10:52 +10:00
|
|
|
}
|
2018-02-05 21:27:32 +11:00
|
|
|
return false;
|
2017-04-06 05:10:52 +10:00
|
|
|
}
|
|
|
|
|
2017-02-17 05:52:55 +11:00
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
TypedValue value = new TypedValue();
|
|
|
|
int color;
|
|
|
|
if (getTheme().resolveAttribute(R.attr.toolbar_icon_tint, value, true)) {
|
|
|
|
color = value.data;
|
|
|
|
} else {
|
|
|
|
color = Color.WHITE;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < menu.size(); i++) {
|
|
|
|
Drawable icon = menu.getItem(i).getIcon();
|
|
|
|
if (icon != null) {
|
|
|
|
icon.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return super.onCreateOptionsMenu(menu);
|
|
|
|
}
|
2017-03-16 09:45:59 +11:00
|
|
|
|
|
|
|
protected void enablePushNotifications() {
|
2017-11-02 07:02:44 +11:00
|
|
|
// schedule job to pull notifications
|
2017-07-09 10:59:48 +10:00
|
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
String minutesString = preferences.getString("pullNotificationCheckInterval", "15");
|
|
|
|
long minutes = Long.valueOf(minutesString);
|
2017-11-02 07:02:44 +11:00
|
|
|
if (minutes < 15) {
|
|
|
|
preferences.edit().putString("pullNotificationCheckInterval", "15").apply();
|
|
|
|
minutes = 15;
|
|
|
|
}
|
|
|
|
setPullNotificationCheckInterval(minutes);
|
2017-03-16 09:45:59 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void disablePushNotifications() {
|
2017-07-09 10:59:48 +10:00
|
|
|
// Cancel the repeating call for "pull" notifications.
|
2017-11-02 07:02:44 +11:00
|
|
|
JobManager.instance().cancelAllForTag(NotificationPullJobCreator.NOTIFICATIONS_JOB_TAG);
|
2017-07-09 10:59:48 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void setPullNotificationCheckInterval(long minutes) {
|
|
|
|
long checkInterval = 1000 * 60 * minutes;
|
2017-11-02 07:02:44 +11:00
|
|
|
|
|
|
|
new JobRequest.Builder(NotificationPullJobCreator.NOTIFICATIONS_JOB_TAG)
|
|
|
|
.setPeriodic(checkInterval)
|
2018-02-13 08:03:08 +11:00
|
|
|
.setUpdateCurrent(true)
|
2017-11-02 07:02:44 +11:00
|
|
|
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
|
|
|
|
.build()
|
2018-03-03 23:54:58 +11:00
|
|
|
.scheduleAsync();
|
2017-03-16 09:45:59 +11:00
|
|
|
}
|
2017-02-17 05:52:55 +11:00
|
|
|
}
|