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-03-09 09:19:03 +11:00
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.GsonBuilder;
|
2017-05-20 16:39:29 +10:00
|
|
|
import com.keylesspalace.tusky.entity.Session;
|
2017-05-05 08:55:34 +10:00
|
|
|
import com.keylesspalace.tusky.json.SpannedTypeAdapter;
|
|
|
|
import com.keylesspalace.tusky.json.StringWithEmoji;
|
|
|
|
import com.keylesspalace.tusky.json.StringWithEmojiTypeAdapter;
|
|
|
|
import com.keylesspalace.tusky.network.MastodonAPI;
|
2017-05-20 11:28:12 +10:00
|
|
|
import com.keylesspalace.tusky.network.TuskyApi;
|
|
|
|
import com.keylesspalace.tusky.util.Log;
|
2017-05-05 08:55:34 +10:00
|
|
|
import com.keylesspalace.tusky.util.OkHttpUtils;
|
2017-05-17 12:19:34 +10:00
|
|
|
import com.keylesspalace.tusky.util.PushNotificationClient;
|
2017-03-09 09:19:03 +11:00
|
|
|
|
2017-03-09 08:34:13 +11:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2017-03-15 11:34:27 +11:00
|
|
|
import okhttp3.Dispatcher;
|
2017-03-09 08:34:13 +11:00
|
|
|
import okhttp3.Interceptor;
|
|
|
|
import okhttp3.OkHttpClient;
|
|
|
|
import okhttp3.Request;
|
|
|
|
import okhttp3.Response;
|
2017-05-20 11:28:12 +10:00
|
|
|
import okhttp3.ResponseBody;
|
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
2017-03-09 08:34:13 +11:00
|
|
|
import retrofit2.Retrofit;
|
|
|
|
import retrofit2.converter.gson.GsonConverterFactory;
|
|
|
|
|
2017-02-17 05:52:55 +11:00
|
|
|
public class BaseActivity extends AppCompatActivity {
|
2017-05-20 11:28:12 +10:00
|
|
|
private static final String TAG = "BaseActivity"; // logging tag
|
|
|
|
|
2017-05-05 08:55:34 +10:00
|
|
|
public MastodonAPI mastodonAPI;
|
2017-05-20 11:28:12 +10:00
|
|
|
public TuskyApi tuskyApi;
|
2017-05-17 12:19:34 +10:00
|
|
|
protected PushNotificationClient pushNotificationClient;
|
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-04-06 05:10:52 +10:00
|
|
|
redirectIfNotLoggedIn();
|
2017-03-09 10:27:37 +11:00
|
|
|
createMastodonAPI();
|
2017-05-20 11:28:12 +10:00
|
|
|
createTuskyApi();
|
|
|
|
createPushNotificationClient();
|
2017-03-09 10:27:37 +11:00
|
|
|
|
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. */
|
2017-02-17 05:52:55 +11:00
|
|
|
if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("lightTheme", false)) {
|
|
|
|
setTheme(R.style.AppTheme_Light);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-15 11:34:27 +11:00
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
2017-03-16 06:32:26 +11: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 getAccessToken() {
|
2017-04-21 08:56:36 +10:00
|
|
|
SharedPreferences preferences = getPrivatePreferences();
|
2017-03-09 08:34:13 +11:00
|
|
|
return preferences.getString("accessToken", null);
|
|
|
|
}
|
|
|
|
|
2017-03-16 09:45:59 +11:00
|
|
|
protected boolean arePushNotificationsEnabled() {
|
2017-03-20 13:38:39 +11:00
|
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
2017-03-16 09:45:59 +11:00
|
|
|
return preferences.getBoolean("notificationsEnabled", true);
|
|
|
|
}
|
|
|
|
|
2017-03-09 08:34:13 +11:00
|
|
|
protected String getBaseUrl() {
|
2017-04-21 08:56:36 +10:00
|
|
|
SharedPreferences preferences = getPrivatePreferences();
|
2017-03-09 08:34:13 +11:00
|
|
|
return "https://" + preferences.getString("domain", null);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void createMastodonAPI() {
|
2017-03-15 11:34:27 +11:00
|
|
|
mastodonApiDispatcher = new Dispatcher();
|
|
|
|
|
2017-03-09 09:19:03 +11:00
|
|
|
Gson gson = new GsonBuilder()
|
|
|
|
.registerTypeAdapter(Spanned.class, new SpannedTypeAdapter())
|
2017-04-26 08:31:27 +10:00
|
|
|
.registerTypeAdapter(StringWithEmoji.class, new StringWithEmojiTypeAdapter())
|
2017-03-09 09:19:03 +11:00
|
|
|
.create();
|
|
|
|
|
2017-04-12 07:07:56 +10:00
|
|
|
OkHttpClient okHttpClient = OkHttpUtils.getCompatibleClientBuilder()
|
2017-03-09 08:34:13 +11:00
|
|
|
.addInterceptor(new Interceptor() {
|
|
|
|
@Override
|
|
|
|
public Response intercept(Chain chain) throws IOException {
|
|
|
|
Request originalRequest = chain.request();
|
|
|
|
|
2017-03-10 13:55:07 +11:00
|
|
|
Request.Builder builder = originalRequest.newBuilder();
|
|
|
|
String accessToken = getAccessToken();
|
|
|
|
if (accessToken != null) {
|
2017-04-12 07:07:56 +10:00
|
|
|
builder.header("Authorization", String.format("Bearer %s",
|
|
|
|
accessToken));
|
2017-03-10 13:55:07 +11:00
|
|
|
}
|
2017-03-09 08:34:13 +11:00
|
|
|
Request newRequest = builder.build();
|
|
|
|
|
|
|
|
return chain.proceed(newRequest);
|
|
|
|
}
|
|
|
|
})
|
2017-03-15 11:34:27 +11:00
|
|
|
.dispatcher(mastodonApiDispatcher)
|
2017-03-09 08:34:13 +11:00
|
|
|
.build();
|
|
|
|
|
|
|
|
Retrofit retrofit = new Retrofit.Builder()
|
|
|
|
.baseUrl(getBaseUrl())
|
|
|
|
.client(okHttpClient)
|
2017-03-09 09:19:03 +11:00
|
|
|
.addConverterFactory(GsonConverterFactory.create(gson))
|
2017-03-09 08:34:13 +11:00
|
|
|
.build();
|
|
|
|
|
|
|
|
mastodonAPI = retrofit.create(MastodonAPI.class);
|
|
|
|
}
|
|
|
|
|
2017-05-20 11:28:12 +10:00
|
|
|
protected void createTuskyApi() {
|
|
|
|
Retrofit retrofit = new Retrofit.Builder()
|
2017-05-22 16:05:37 +10:00
|
|
|
.baseUrl("https://" + getString(R.string.tusky_api_domain) + ":8080")
|
2017-05-20 11:28:12 +10:00
|
|
|
.client(OkHttpUtils.getCompatibleClient())
|
2017-05-20 16:39:29 +10:00
|
|
|
.addConverterFactory(GsonConverterFactory.create())
|
2017-05-20 11:28:12 +10:00
|
|
|
.build();
|
|
|
|
|
|
|
|
tuskyApi = retrofit.create(TuskyApi.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void createPushNotificationClient() {
|
2017-05-20 10:00:57 +10:00
|
|
|
pushNotificationClient = new PushNotificationClient(getApplicationContext(),
|
2017-05-22 16:05:37 +10:00
|
|
|
"ssl://" + getString(R.string.tusky_api_domain) + ":8883");
|
2017-03-12 18:31:20 +11:00
|
|
|
}
|
|
|
|
|
2017-04-06 05:10:52 +10:00
|
|
|
protected void redirectIfNotLoggedIn() {
|
2017-04-21 08:56:36 +10:00
|
|
|
SharedPreferences preferences = getPrivatePreferences();
|
2017-04-06 05:10:52 +10:00
|
|
|
String domain = preferences.getString("domain", null);
|
|
|
|
String accessToken = preferences.getString("accessToken", null);
|
2017-04-06 06:35:22 +10:00
|
|
|
if (domain == null || accessToken == null) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-05-20 11:28:12 +10:00
|
|
|
Callback<ResponseBody> callback = new Callback<ResponseBody>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(Call<ResponseBody> call,
|
|
|
|
retrofit2.Response<ResponseBody> response) {
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
pushNotificationClient.subscribeToTopic(getPushNotificationTopic());
|
2017-05-22 16:05:37 +10:00
|
|
|
pushNotificationClient.connect(BaseActivity.this);
|
2017-05-20 11:28:12 +10:00
|
|
|
} else {
|
2017-05-22 16:05:37 +10:00
|
|
|
onEnablePushNotificationsFailure(response.message());
|
2017-05-20 11:28:12 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Call<ResponseBody> call, Throwable t) {
|
2017-05-22 16:05:37 +10:00
|
|
|
onEnablePushNotificationsFailure(t.getMessage());
|
2017-05-20 11:28:12 +10:00
|
|
|
}
|
|
|
|
};
|
2017-05-20 16:39:29 +10:00
|
|
|
String deviceToken = pushNotificationClient.getDeviceToken();
|
|
|
|
Session session = new Session(getDomain(), getAccessToken(), deviceToken);
|
|
|
|
tuskyApi.register(session)
|
2017-05-20 11:28:12 +10:00
|
|
|
.enqueue(callback);
|
|
|
|
}
|
|
|
|
|
2017-05-22 16:05:37 +10:00
|
|
|
private void onEnablePushNotificationsFailure(String message) {
|
|
|
|
Log.e(TAG, "Enabling push notifications failed. " + message);
|
2017-03-16 09:45:59 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void disablePushNotifications() {
|
2017-05-20 11:28:12 +10:00
|
|
|
Callback<ResponseBody> callback = new Callback<ResponseBody>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(Call<ResponseBody> call,
|
|
|
|
retrofit2.Response<ResponseBody> response) {
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
pushNotificationClient.unsubscribeToTopic(getPushNotificationTopic());
|
|
|
|
} else {
|
|
|
|
onDisablePushNotificationsFailure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Call<ResponseBody> call, Throwable t) {
|
|
|
|
onDisablePushNotificationsFailure();
|
|
|
|
}
|
|
|
|
};
|
2017-05-20 16:39:29 +10:00
|
|
|
String deviceToken = pushNotificationClient.getDeviceToken();
|
|
|
|
Session session = new Session(getDomain(), getAccessToken(), deviceToken);
|
|
|
|
tuskyApi.unregister(session)
|
2017-05-20 11:28:12 +10:00
|
|
|
.enqueue(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onDisablePushNotificationsFailure() {
|
|
|
|
Log.e(TAG, "Disabling push notifications failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getPushNotificationTopic() {
|
2017-05-20 16:39:29 +10:00
|
|
|
return String.format("%s/%s/#", getDomain(), getAccessToken());
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getDomain() {
|
|
|
|
return getPrivatePreferences()
|
|
|
|
.getString("domain", null);
|
2017-03-16 09:45:59 +11:00
|
|
|
}
|
2017-02-17 05:52:55 +11:00
|
|
|
}
|