2017-02-16 19:52:55 +01:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
|
|
|
* This file is part of Tusky.
|
|
|
|
*
|
|
|
|
* Tusky 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
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2017-03-08 22:34:13 +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;
|
2017-02-16 19:52:55 +01:00
|
|
|
import android.graphics.Color;
|
|
|
|
import android.graphics.PorterDuff;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.v7.app.AppCompatActivity;
|
2017-03-08 23:19:03 +01:00
|
|
|
import android.text.Spanned;
|
2017-02-16 19:52:55 +01:00
|
|
|
import android.util.TypedValue;
|
|
|
|
import android.view.Menu;
|
|
|
|
|
2017-03-08 23:19:03 +01:00
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
|
2017-03-08 22:34:13 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2017-03-15 01:34:27 +01:00
|
|
|
import okhttp3.Dispatcher;
|
2017-03-08 22:34:13 +01:00
|
|
|
import okhttp3.Interceptor;
|
|
|
|
import okhttp3.OkHttpClient;
|
|
|
|
import okhttp3.Request;
|
|
|
|
import okhttp3.Response;
|
|
|
|
import retrofit2.Retrofit;
|
|
|
|
import retrofit2.converter.gson.GsonConverterFactory;
|
|
|
|
|
2017-02-16 19:52:55 +01: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. And
|
|
|
|
* the most expedient way to accomplish this was to put it in a base class and just have every
|
|
|
|
* activity extend from it. */
|
|
|
|
public class BaseActivity extends AppCompatActivity {
|
2017-03-08 22:34:13 +01:00
|
|
|
protected MastodonAPI mastodonAPI;
|
2017-03-12 08:31:20 +01:00
|
|
|
protected TuskyAPI tuskyAPI;
|
2017-03-15 01:34:27 +01:00
|
|
|
protected Dispatcher mastodonApiDispatcher;
|
2017-03-08 22:34:13 +01: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
|
|
|
|
|
|
|
createMastodonAPI();
|
2017-03-12 08:31:20 +01:00
|
|
|
createTuskyAPI();
|
2017-03-09 00:27:37 +01:00
|
|
|
|
2017-03-14 01:49:12 +01:00
|
|
|
/* Note from Andrew March 13, 2017: Keep this and restore it when the light theme is no
|
|
|
|
longer bugged.
|
2017-02-16 19:52:55 +01:00
|
|
|
if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("lightTheme", false)) {
|
|
|
|
setTheme(R.style.AppTheme_Light);
|
|
|
|
}
|
2017-03-14 01:49:12 +01:00
|
|
|
*/
|
2017-02-16 19:52:55 +01:00
|
|
|
}
|
|
|
|
|
2017-03-15 01:34:27 +01:00
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
2017-03-15 20:32:26 +01:00
|
|
|
if(mastodonApiDispatcher != null) mastodonApiDispatcher.cancelAll();
|
2017-03-15 01:34:27 +01:00
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
2017-03-07 13:05:51 +01: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-03-08 22:34:13 +01:00
|
|
|
protected String getAccessToken() {
|
|
|
|
SharedPreferences preferences = getSharedPreferences(getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
|
|
|
|
return preferences.getString("accessToken", null);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String getBaseUrl() {
|
|
|
|
SharedPreferences preferences = getSharedPreferences(getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
|
|
|
|
return "https://" + preferences.getString("domain", null);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void createMastodonAPI() {
|
2017-03-15 01:34:27 +01:00
|
|
|
mastodonApiDispatcher = new Dispatcher();
|
|
|
|
|
2017-03-08 22:34:13 +01:00
|
|
|
OkHttpClient okHttpClient = new OkHttpClient.Builder()
|
|
|
|
.addInterceptor(new Interceptor() {
|
|
|
|
@Override
|
|
|
|
public Response intercept(Chain chain) throws IOException {
|
|
|
|
Request originalRequest = chain.request();
|
|
|
|
|
2017-03-10 03:55:07 +01:00
|
|
|
Request.Builder builder = originalRequest.newBuilder();
|
|
|
|
String accessToken = getAccessToken();
|
|
|
|
if (accessToken != null) {
|
|
|
|
builder.header("Authorization", String.format("Bearer %s", accessToken));
|
|
|
|
}
|
2017-03-08 22:34:13 +01:00
|
|
|
Request newRequest = builder.build();
|
|
|
|
|
|
|
|
return chain.proceed(newRequest);
|
|
|
|
}
|
|
|
|
})
|
2017-03-15 01:34:27 +01:00
|
|
|
.dispatcher(mastodonApiDispatcher)
|
2017-03-08 22:34:13 +01:00
|
|
|
.build();
|
|
|
|
|
2017-03-08 23:19:03 +01:00
|
|
|
Gson gson = new GsonBuilder()
|
|
|
|
.registerTypeAdapter(Spanned.class, new SpannedTypeAdapter())
|
|
|
|
.create();
|
|
|
|
|
2017-03-08 22:34:13 +01:00
|
|
|
Retrofit retrofit = new Retrofit.Builder()
|
|
|
|
.baseUrl(getBaseUrl())
|
|
|
|
.client(okHttpClient)
|
2017-03-08 23:19:03 +01:00
|
|
|
.addConverterFactory(GsonConverterFactory.create(gson))
|
2017-03-08 22:34:13 +01:00
|
|
|
.build();
|
|
|
|
|
|
|
|
mastodonAPI = retrofit.create(MastodonAPI.class);
|
|
|
|
}
|
|
|
|
|
2017-03-12 08:31:20 +01:00
|
|
|
protected void createTuskyAPI() {
|
|
|
|
Retrofit retrofit = new Retrofit.Builder()
|
|
|
|
.baseUrl(getString(R.string.tusky_api_url))
|
|
|
|
.build();
|
|
|
|
|
|
|
|
tuskyAPI = retrofit.create(TuskyAPI.class);
|
|
|
|
}
|
|
|
|
|
2017-02-16 19:52:55 +01: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);
|
|
|
|
}
|
|
|
|
}
|