BaseActivity.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* Copyright 2017 Andrew Dawson
  2. *
  3. * This file is a part of Tusky.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it under the terms of the
  6. * GNU General Public License as published by the Free Software Foundation; either version 3 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  10. * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. * Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with Tusky; if not,
  14. * see <http://www.gnu.org/licenses>. */
  15. package com.keylesspalace.tusky;
  16. import android.content.Context;
  17. import android.content.Intent;
  18. import android.content.SharedPreferences;
  19. import android.graphics.Color;
  20. import android.graphics.PorterDuff;
  21. import android.graphics.drawable.Drawable;
  22. import android.os.Bundle;
  23. import android.preference.PreferenceManager;
  24. import android.support.annotation.Nullable;
  25. import android.support.v7.app.AppCompatActivity;
  26. import android.text.Spanned;
  27. import android.util.TypedValue;
  28. import android.view.Menu;
  29. import com.google.firebase.iid.FirebaseInstanceId;
  30. import com.google.gson.Gson;
  31. import com.google.gson.GsonBuilder;
  32. import java.io.IOException;
  33. import okhttp3.Dispatcher;
  34. import okhttp3.Interceptor;
  35. import okhttp3.OkHttpClient;
  36. import okhttp3.Request;
  37. import okhttp3.Response;
  38. import okhttp3.ResponseBody;
  39. import retrofit2.Call;
  40. import retrofit2.Callback;
  41. import retrofit2.Retrofit;
  42. import retrofit2.converter.gson.GsonConverterFactory;
  43. /* There isn't presently a way to globally change the theme of a whole application at runtime, just
  44. * individual activities. So, each activity has to set its theme before any views are created. And
  45. * the most expedient way to accomplish this was to put it in a base class and just have every
  46. * activity extend from it. */
  47. public class BaseActivity extends AppCompatActivity {
  48. private static final String TAG = "BaseActivity"; // logging tag
  49. protected MastodonAPI mastodonAPI;
  50. protected TuskyAPI tuskyAPI;
  51. protected Dispatcher mastodonApiDispatcher;
  52. @Override
  53. protected void onCreate(@Nullable Bundle savedInstanceState) {
  54. super.onCreate(savedInstanceState);
  55. redirectIfNotLoggedIn();
  56. createMastodonAPI();
  57. createTuskyAPI();
  58. if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("lightTheme", false)) {
  59. setTheme(R.style.AppTheme_Light);
  60. }
  61. }
  62. @Override
  63. protected void onDestroy() {
  64. if(mastodonApiDispatcher != null) mastodonApiDispatcher.cancelAll();
  65. super.onDestroy();
  66. }
  67. @Override
  68. public void finish() {
  69. super.finish();
  70. overridePendingTransitionExit();
  71. }
  72. @Override
  73. public void startActivity(Intent intent) {
  74. super.startActivity(intent);
  75. overridePendingTransitionEnter();
  76. }
  77. private void overridePendingTransitionEnter() {
  78. overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left);
  79. }
  80. private void overridePendingTransitionExit() {
  81. overridePendingTransition(R.anim.slide_from_left, R.anim.slide_to_right);
  82. }
  83. protected String getAccessToken() {
  84. SharedPreferences preferences = getSharedPreferences(getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
  85. return preferences.getString("accessToken", null);
  86. }
  87. protected boolean arePushNotificationsEnabled() {
  88. SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
  89. return preferences.getBoolean("notificationsEnabled", true);
  90. }
  91. protected String getBaseUrl() {
  92. SharedPreferences preferences = getSharedPreferences(getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
  93. return "https://" + preferences.getString("domain", null);
  94. }
  95. protected void createMastodonAPI() {
  96. mastodonApiDispatcher = new Dispatcher();
  97. OkHttpClient okHttpClient = new OkHttpClient.Builder()
  98. .addInterceptor(new Interceptor() {
  99. @Override
  100. public Response intercept(Chain chain) throws IOException {
  101. Request originalRequest = chain.request();
  102. Request.Builder builder = originalRequest.newBuilder();
  103. String accessToken = getAccessToken();
  104. if (accessToken != null) {
  105. builder.header("Authorization", String.format("Bearer %s", accessToken));
  106. }
  107. Request newRequest = builder.build();
  108. return chain.proceed(newRequest);
  109. }
  110. })
  111. .dispatcher(mastodonApiDispatcher)
  112. .build();
  113. Gson gson = new GsonBuilder()
  114. .registerTypeAdapter(Spanned.class, new SpannedTypeAdapter())
  115. .create();
  116. Retrofit retrofit = new Retrofit.Builder()
  117. .baseUrl(getBaseUrl())
  118. .client(okHttpClient)
  119. .addConverterFactory(GsonConverterFactory.create(gson))
  120. .build();
  121. mastodonAPI = retrofit.create(MastodonAPI.class);
  122. }
  123. protected void createTuskyAPI() {
  124. Retrofit retrofit = new Retrofit.Builder()
  125. .baseUrl(getString(R.string.tusky_api_url))
  126. .build();
  127. tuskyAPI = retrofit.create(TuskyAPI.class);
  128. }
  129. protected void redirectIfNotLoggedIn() {
  130. SharedPreferences preferences = getSharedPreferences(
  131. getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
  132. String domain = preferences.getString("domain", null);
  133. String accessToken = preferences.getString("accessToken", null);
  134. if (domain == null || accessToken == null) {
  135. Intent intent = new Intent(this, LoginActivity.class);
  136. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
  137. startActivity(intent);
  138. finish();
  139. }
  140. }
  141. @Override
  142. public boolean onCreateOptionsMenu(Menu menu) {
  143. TypedValue value = new TypedValue();
  144. int color;
  145. if (getTheme().resolveAttribute(R.attr.toolbar_icon_tint, value, true)) {
  146. color = value.data;
  147. } else {
  148. color = Color.WHITE;
  149. }
  150. for (int i = 0; i < menu.size(); i++) {
  151. Drawable icon = menu.getItem(i).getIcon();
  152. if (icon != null) {
  153. icon.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
  154. }
  155. }
  156. return super.onCreateOptionsMenu(menu);
  157. }
  158. protected void enablePushNotifications() {
  159. tuskyAPI.register(getBaseUrl(), getAccessToken(), FirebaseInstanceId.getInstance().getToken()).enqueue(new Callback<ResponseBody>() {
  160. @Override
  161. public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
  162. Log.d(TAG, "Enable push notifications response: " + response.message());
  163. }
  164. @Override
  165. public void onFailure(Call<ResponseBody> call, Throwable t) {
  166. Log.d(TAG, "Enable push notifications failed: " + t.getMessage());
  167. }
  168. });
  169. }
  170. protected void disablePushNotifications() {
  171. tuskyAPI.unregister(getBaseUrl(), getAccessToken()).enqueue(new Callback<ResponseBody>() {
  172. @Override
  173. public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
  174. Log.d(TAG, "Disable push notifications response: " + response.message());
  175. }
  176. @Override
  177. public void onFailure(Call<ResponseBody> call, Throwable t) {
  178. Log.d(TAG, "Disable push notifications failed: " + t.getMessage());
  179. }
  180. });
  181. }
  182. }