chinwag-android/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java

214 lines
7.3 KiB
Java
Raw Normal View History

/* Copyright 2017 Andrew Dawson
*
2017-04-10 10:12:31 +10:00
* This file is a part of Tusky.
*
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.
*
* 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-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>. */
package com.keylesspalace.tusky;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Spanned;
import android.util.TypedValue;
import android.view.Menu;
import com.evernote.android.job.JobManager;
import com.evernote.android.job.JobRequest;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.keylesspalace.tusky.db.AccountEntity;
2017-05-05 08:55:34 +10:00
import com.keylesspalace.tusky.json.SpannedTypeAdapter;
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;
Theming improvements (#502) * Split theme definitions into day and night * Add support for Night Mode in code * Add theme chooser in preferences * Fix translations * Adjust IDs * Adjust preferences for custom themes * UI tweaks for custom theme support * Added code for custom theme support 🍅 * Fixed resource display in Kotlin 🍅 * Restored styles * Updated strings * Fixed getIdentifier() to fit into setTheme() * Removed redundant resources * Reset default theme to "Dusky" * Fixed night mode handler to maintain compatibility * Refactor functions to use helper methods * Added license block * Added preview to theme selector * Added color identifier getter helper method * Fixed reference in AccountMediaFragment * Cleanup * Fixed navbar foreground not changing color * Fix fallback theme switch(){} * Enable location-based daylight trigger * Cleanup * Modified theming strategy to reduce clutter in preferences * Updated translations for latest version * Removed "Default" theme flavor from settings * Updated Polish translations 🇵🇱 * Modified TwilightManager handling code to support Android M's UiModeManager features and moved it to its own function * Updated Polish translations 🇵🇱 * Cleanup; Fixed hardcoded string * Added missing escape in string * Removed permission request dialog. As we now use native UiModeManager APIs that don't need special permission for Android 6.0 and above, we no longer need to bother user with Android M+ specific location permission request dialog. * Increased readability of ThemeUtil class * Refactored ThemeUtils.setAppNightMode method * Cleanup
2018-01-20 23:39:01 +11:00
import com.keylesspalace.tusky.util.ThemeUtils;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
2017-10-20 00:25:04 +11:00
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public abstract class BaseActivity extends AppCompatActivity {
2017-06-23 04:01:25 +10:00
public MastodonApi mastodonApi;
protected Dispatcher mastodonApiDispatcher;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
/* 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);
int style;
switch(preferences.getString("statusTextSize", "medium")) {
case "large":
style = R.style.TextSizeLarge;
break;
case "small":
style = R.style.TextSizeSmall;
break;
case "medium":
default:
style = R.style.TextSizeMedium;
break;
}
getTheme().applyStyle(style, false);
if(redirectIfNotLoggedIn()) {
return;
}
createMastodonApi();
}
@Override
protected void onDestroy() {
2017-06-23 04:01:25 +10:00
if (mastodonApiDispatcher != null) {
mastodonApiDispatcher.cancelAll();
}
super.onDestroy();
}
@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);
}
protected String getBaseUrl() {
AccountEntity account = TuskyApplication.getAccountManager().getActiveAccount();
if(account != null) {
return "https://" + account.getDomain();
} else {
return "";
}
}
2017-06-23 04:01:25 +10:00
protected void createMastodonApi() {
mastodonApiDispatcher = new Dispatcher();
2017-10-20 00:54:08 +11:00
Gson gson = new GsonBuilder()
.registerTypeAdapter(Spanned.class, new SpannedTypeAdapter())
.create();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
2017-10-20 00:25:04 +11:00
OkHttpClient.Builder okBuilder =
OkHttpUtils.getCompatibleClientBuilder(preferences)
.addInterceptor(new AuthInterceptor())
.dispatcher(mastodonApiDispatcher);
2017-10-20 00:25:04 +11:00
if (BuildConfig.DEBUG) {
okBuilder.addInterceptor(
new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC));
2017-10-20 00:25:04 +11:00
}
2017-10-20 00:25:04 +11:00
Retrofit retrofit = new Retrofit.Builder().baseUrl(getBaseUrl())
.client(okBuilder.build())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
2017-06-23 04:01:25 +10:00
mastodonApi = retrofit.create(MastodonApi.class);
}
protected boolean redirectIfNotLoggedIn() {
if (TuskyApplication.getAccountManager().getActiveAccount() == null) {
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
return true;
}
return false;
}
@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);
}
protected void enablePushNotifications() {
// schedule job to pull notifications
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String minutesString = preferences.getString("pullNotificationCheckInterval", "15");
long minutes = Long.valueOf(minutesString);
if (minutes < 15) {
preferences.edit().putString("pullNotificationCheckInterval", "15").apply();
minutes = 15;
}
setPullNotificationCheckInterval(minutes);
}
protected void disablePushNotifications() {
// Cancel the repeating call for "pull" notifications.
JobManager.instance().cancelAllForTag(NotificationPullJobCreator.NOTIFICATIONS_JOB_TAG);
}
protected void setPullNotificationCheckInterval(long minutes) {
long checkInterval = 1000 * 60 * minutes;
new JobRequest.Builder(NotificationPullJobCreator.NOTIFICATIONS_JOB_TAG)
.setPeriodic(checkInterval)
.setUpdateCurrent(true)
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
.build()
.scheduleAsync();
}
}