2017-01-20 19:09:10 +11: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/>. */
|
|
|
|
|
2017-01-03 10:30:27 +11:00
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
2017-01-25 15:35:54 +11:00
|
|
|
import android.app.AlarmManager;
|
|
|
|
import android.app.PendingIntent;
|
2017-01-03 10:30:27 +11:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
2017-02-17 13:11:05 +11:00
|
|
|
import android.graphics.drawable.Drawable;
|
2017-03-07 23:05:51 +11:00
|
|
|
import android.os.Build;
|
2017-01-25 15:35:54 +11:00
|
|
|
import android.os.SystemClock;
|
2017-02-05 18:34:55 +11:00
|
|
|
import android.preference.PreferenceManager;
|
2017-03-07 23:05:51 +11:00
|
|
|
import android.support.annotation.RequiresApi;
|
2017-03-07 11:38:22 +11:00
|
|
|
import android.support.design.widget.FloatingActionButton;
|
2017-01-08 09:24:02 +11:00
|
|
|
import android.support.design.widget.TabLayout;
|
|
|
|
import android.support.v4.view.ViewPager;
|
2017-01-03 10:30:27 +11:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.v7.widget.Toolbar;
|
2017-03-07 23:05:51 +11:00
|
|
|
import android.transition.Slide;
|
|
|
|
import android.transition.TransitionInflater;
|
2017-01-03 10:30:27 +11:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2017-03-07 11:38:22 +11:00
|
|
|
import android.view.View;
|
2017-01-03 10:30:27 +11:00
|
|
|
|
2017-01-28 14:33:43 +11:00
|
|
|
import com.android.volley.AuthFailureError;
|
|
|
|
import com.android.volley.Request;
|
|
|
|
import com.android.volley.Response;
|
|
|
|
import com.android.volley.VolleyError;
|
|
|
|
import com.android.volley.toolbox.JsonObjectRequest;
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2017-03-07 21:02:41 +11:00
|
|
|
import java.util.Stack;
|
2017-01-28 14:33:43 +11:00
|
|
|
|
2017-02-17 05:52:55 +11:00
|
|
|
public class MainActivity extends BaseActivity {
|
2017-02-24 11:32:45 +11:00
|
|
|
private static final String TAG = "MainActivity"; // logging tag and Volley request tag
|
2017-01-31 15:51:02 +11:00
|
|
|
|
2017-01-25 15:35:54 +11:00
|
|
|
private AlarmManager alarmManager;
|
|
|
|
private PendingIntent serviceAlarmIntent;
|
|
|
|
private boolean notificationServiceEnabled;
|
2017-01-28 14:33:43 +11:00
|
|
|
private String loggedInAccountId;
|
|
|
|
private String loggedInAccountUsername;
|
2017-03-07 21:02:41 +11:00
|
|
|
Stack<Integer> pageHistory = new Stack<Integer>();
|
|
|
|
private ViewPager viewPager;
|
2017-01-25 15:35:54 +11:00
|
|
|
|
2017-01-03 10:30:27 +11:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_main);
|
2017-01-08 09:24:02 +11:00
|
|
|
|
2017-01-28 14:33:43 +11:00
|
|
|
// Fetch user info while we're doing other things.
|
|
|
|
fetchUserInfo();
|
|
|
|
|
2017-01-03 10:30:27 +11:00
|
|
|
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
|
|
|
setSupportActionBar(toolbar);
|
|
|
|
|
2017-03-07 11:38:22 +11:00
|
|
|
FloatingActionButton floatingBtn = (FloatingActionButton) findViewById(R.id.floating_btn);
|
|
|
|
floatingBtn.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
Intent intent = new Intent(getApplicationContext(), ComposeActivity.class);
|
|
|
|
startActivity(intent);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-01-25 15:35:54 +11:00
|
|
|
// Setup the tabs and timeline pager.
|
2017-02-23 06:13:51 +11:00
|
|
|
TimelinePagerAdapter adapter = new TimelinePagerAdapter(getSupportFragmentManager());
|
2017-01-08 09:24:02 +11:00
|
|
|
String[] pageTitles = {
|
|
|
|
getString(R.string.title_home),
|
|
|
|
getString(R.string.title_notifications),
|
|
|
|
getString(R.string.title_public)
|
2017-01-04 11:23:57 +11:00
|
|
|
};
|
2017-01-08 09:24:02 +11:00
|
|
|
adapter.setPageTitles(pageTitles);
|
2017-03-07 21:02:41 +11:00
|
|
|
viewPager = (ViewPager) findViewById(R.id.pager);
|
2017-02-13 16:18:17 +11:00
|
|
|
int pageMargin = getResources().getDimensionPixelSize(R.dimen.tab_page_margin);
|
2017-02-03 11:18:41 +11:00
|
|
|
viewPager.setPageMargin(pageMargin);
|
2017-02-17 13:11:05 +11:00
|
|
|
Drawable pageMarginDrawable = ThemeUtils.getDrawable(this, R.attr.tab_page_margin_drawable,
|
|
|
|
R.drawable.tab_page_margin_dark);
|
2017-03-03 11:25:35 +11:00
|
|
|
viewPager.setPageMarginDrawable(pageMarginDrawable);
|
2017-01-08 09:24:02 +11:00
|
|
|
viewPager.setAdapter(adapter);
|
2017-03-07 21:02:41 +11:00
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
|
|
|
|
tabLayout.setupWithViewPager(viewPager);
|
2017-01-25 15:35:54 +11:00
|
|
|
|
2017-03-07 21:02:41 +11:00
|
|
|
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
|
|
|
|
@Override
|
|
|
|
public void onTabSelected(TabLayout.Tab tab) {
|
|
|
|
viewPager.setCurrentItem(tab.getPosition());
|
|
|
|
|
|
|
|
if (pageHistory.empty()) {
|
|
|
|
pageHistory.push(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pageHistory.contains(tab.getPosition())) {
|
|
|
|
pageHistory.remove(pageHistory.indexOf(tab.getPosition()));
|
|
|
|
}
|
|
|
|
|
|
|
|
pageHistory.push(tab.getPosition());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTabUnselected(TabLayout.Tab tab) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTabReselected(TabLayout.Tab tab) {
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-01-25 15:35:54 +11:00
|
|
|
// Retrieve notification update preference.
|
2017-02-05 18:34:55 +11:00
|
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
notificationServiceEnabled = preferences.getBoolean("pullNotifications", true);
|
|
|
|
String minutesString = preferences.getString("pullNotificationCheckInterval", "15");
|
|
|
|
long notificationCheckInterval = 60 * 1000 * Integer.valueOf(minutesString);
|
2017-01-31 15:51:02 +11:00
|
|
|
// Start up the PullNotificationsService.
|
2017-01-25 15:35:54 +11:00
|
|
|
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
2017-01-31 15:51:02 +11:00
|
|
|
Intent intent = new Intent(this, PullNotificationService.class);
|
2017-01-25 15:35:54 +11:00
|
|
|
final int SERVICE_REQUEST_CODE = 8574603; // This number is arbitrary.
|
|
|
|
serviceAlarmIntent = PendingIntent.getService(this, SERVICE_REQUEST_CODE, intent,
|
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
if (notificationServiceEnabled) {
|
|
|
|
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
|
|
|
|
SystemClock.elapsedRealtime(), notificationCheckInterval, serviceAlarmIntent);
|
|
|
|
} else {
|
|
|
|
alarmManager.cancel(serviceAlarmIntent);
|
|
|
|
}
|
2017-01-03 10:30:27 +11:00
|
|
|
}
|
|
|
|
|
2017-01-28 14:33:43 +11:00
|
|
|
private void fetchUserInfo() {
|
|
|
|
SharedPreferences preferences = getSharedPreferences(
|
|
|
|
getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
|
|
|
|
String domain = preferences.getString("domain", null);
|
|
|
|
final String accessToken = preferences.getString("accessToken", null);
|
|
|
|
String id = preferences.getString("loggedInAccountId", null);
|
|
|
|
String username = preferences.getString("loggedInAccountUsername", null);
|
|
|
|
if (id != null && username != null) {
|
|
|
|
loggedInAccountId = id;
|
|
|
|
loggedInAccountUsername = username;
|
|
|
|
} else {
|
|
|
|
String endpoint = getString(R.string.endpoint_verify_credentials);
|
|
|
|
String url = "https://" + domain + endpoint;
|
|
|
|
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
|
|
|
|
new Response.Listener<JSONObject>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(JSONObject response) {
|
2017-01-31 15:51:02 +11:00
|
|
|
String username;
|
|
|
|
String id;
|
2017-01-28 14:33:43 +11:00
|
|
|
try {
|
2017-01-31 15:51:02 +11:00
|
|
|
id = response.getString("id");
|
|
|
|
username = response.getString("acct");
|
2017-01-28 14:33:43 +11:00
|
|
|
} catch (JSONException e) {
|
2017-02-07 18:05:50 +11:00
|
|
|
onFetchUserInfoFailure(e);
|
2017-01-31 15:51:02 +11:00
|
|
|
return;
|
2017-01-28 14:33:43 +11:00
|
|
|
}
|
2017-01-31 15:51:02 +11:00
|
|
|
onFetchUserInfoSuccess(id, username);
|
2017-01-28 14:33:43 +11:00
|
|
|
}
|
|
|
|
},
|
|
|
|
new Response.ErrorListener() {
|
|
|
|
@Override
|
|
|
|
public void onErrorResponse(VolleyError error) {
|
2017-02-07 18:05:50 +11:00
|
|
|
onFetchUserInfoFailure(error);
|
2017-01-28 14:33:43 +11:00
|
|
|
}
|
|
|
|
}) {
|
|
|
|
@Override
|
|
|
|
public Map<String, String> getHeaders() throws AuthFailureError {
|
|
|
|
Map<String, String> headers = new HashMap<>();
|
|
|
|
headers.put("Authorization", "Bearer " + accessToken);
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
};
|
2017-02-24 11:32:45 +11:00
|
|
|
request.setTag(TAG);
|
2017-01-28 14:33:43 +11:00
|
|
|
VolleySingleton.getInstance(this).addToRequestQueue(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onFetchUserInfoSuccess(String id, String username) {
|
|
|
|
loggedInAccountId = id;
|
|
|
|
loggedInAccountUsername = username;
|
|
|
|
SharedPreferences preferences = getSharedPreferences(
|
|
|
|
getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
|
|
|
|
SharedPreferences.Editor editor = preferences.edit();
|
|
|
|
editor.putString("loggedInAccountId", loggedInAccountId);
|
|
|
|
editor.putString("loggedInAccountUsername", loggedInAccountUsername);
|
|
|
|
editor.apply();
|
|
|
|
}
|
|
|
|
|
2017-02-07 18:05:50 +11:00
|
|
|
private void onFetchUserInfoFailure(Exception exception) {
|
|
|
|
Log.e(TAG, "Failed to fetch user info. " + exception.getMessage());
|
2017-01-31 15:51:02 +11:00
|
|
|
}
|
|
|
|
|
2017-01-03 10:30:27 +11:00
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
getMenuInflater().inflate(R.menu.main_toolbar, menu);
|
|
|
|
return super.onCreateOptionsMenu(menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
2017-02-22 09:55:37 +11:00
|
|
|
case R.id.action_view_profile: {
|
2017-02-21 13:32:10 +11:00
|
|
|
Intent intent = new Intent(this, AccountActivity.class);
|
|
|
|
intent.putExtra("id", loggedInAccountId);
|
|
|
|
startActivity(intent);
|
2017-01-28 14:33:43 +11:00
|
|
|
return true;
|
|
|
|
}
|
2017-02-22 09:55:37 +11:00
|
|
|
case R.id.action_view_preferences: {
|
2017-02-21 13:32:10 +11:00
|
|
|
Intent intent = new Intent(this, PreferencesActivity.class);
|
|
|
|
startActivity(intent);
|
|
|
|
return true;
|
|
|
|
}
|
2017-02-22 09:55:37 +11:00
|
|
|
case R.id.action_view_favourites: {
|
2017-02-21 13:32:10 +11:00
|
|
|
Intent intent = new Intent(this, FavouritesActivity.class);
|
|
|
|
startActivity(intent);
|
2017-02-05 18:34:55 +11:00
|
|
|
return true;
|
|
|
|
}
|
2017-02-22 09:55:37 +11:00
|
|
|
case R.id.action_view_blocks: {
|
|
|
|
Intent intent = new Intent(this, BlocksActivity.class);
|
|
|
|
startActivity(intent);
|
|
|
|
return true;
|
|
|
|
}
|
2017-01-03 10:30:27 +11:00
|
|
|
case R.id.action_logout: {
|
2017-02-21 13:32:10 +11:00
|
|
|
if (notificationServiceEnabled) {
|
|
|
|
alarmManager.cancel(serviceAlarmIntent);
|
|
|
|
}
|
|
|
|
SharedPreferences preferences = getSharedPreferences(
|
|
|
|
getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
|
|
|
|
SharedPreferences.Editor editor = preferences.edit();
|
|
|
|
editor.remove("domain");
|
|
|
|
editor.remove("accessToken");
|
|
|
|
editor.apply();
|
|
|
|
Intent intent = new Intent(this, SplashActivity.class);
|
|
|
|
startActivity(intent);
|
|
|
|
finish();
|
2017-01-03 10:30:27 +11:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2017-03-07 21:02:41 +11:00
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
return super.onOptionsItemSelected(item);
|
2017-01-03 10:30:27 +11:00
|
|
|
}
|
2017-03-07 21:02:41 +11:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
|
|
|
if(pageHistory.empty()) {
|
|
|
|
super.onBackPressed();
|
|
|
|
} else {
|
|
|
|
pageHistory.pop();
|
|
|
|
viewPager.setCurrentItem(pageHistory.lastElement());
|
|
|
|
}
|
|
|
|
}
|
2017-01-03 10:30:27 +11:00
|
|
|
}
|