fedilab-Android-App/app/src/fdroid/java/fr.gouv.etalab.mastodon/activities/MainActivity.java

1080 lines
52 KiB
Java
Raw Normal View History

2017-05-05 16:36:04 +02:00
/* Copyright 2017 Thomas Schneider
*
2017-07-10 10:33:24 +02:00
* This file is a part of Mastalab
2017-05-05 16:36:04 +02: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-07-10 10:33:24 +02:00
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2017-05-05 16:36:04 +02:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2017-08-04 11:11:27 +02:00
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
2017-05-05 16:36:04 +02:00
* see <http://www.gnu.org/licenses>. */
package fr.gouv.etalab.mastodon.activities;
2017-08-24 11:56:29 +02:00
import android.annotation.SuppressLint;
2017-05-05 16:36:04 +02:00
import android.content.Context;
2017-06-19 19:20:20 +02:00
import android.content.DialogInterface;
2017-05-05 16:36:04 +02:00
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.PorterDuff;
import android.net.Uri;
2017-05-05 16:36:04 +02:00
import android.os.AsyncTask;
import android.os.Bundle;
2017-08-25 10:25:13 +02:00
import android.os.Handler;
2017-05-05 16:36:04 +02:00
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
2017-05-05 16:36:04 +02:00
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
2017-06-19 19:20:20 +02:00
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.SearchView;
2017-08-01 15:44:26 +02:00
import android.support.v7.widget.SwitchCompat;
import android.view.LayoutInflater;
2017-05-05 16:36:04 +02:00
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
2017-05-05 16:36:04 +02:00
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
2017-06-19 19:20:20 +02:00
import android.widget.Toast;
2017-05-05 16:36:04 +02:00
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import java.io.File;
import java.util.ArrayList;
2017-08-24 13:03:22 +02:00
import java.util.Date;
2017-05-05 16:36:04 +02:00
import java.util.HashMap;
2017-06-19 19:20:20 +02:00
import java.util.Locale;
import java.util.Stack;
2017-08-24 13:03:22 +02:00
import java.util.concurrent.TimeUnit;
2017-05-05 16:36:04 +02:00
import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoByIDAsyncTask;
2017-05-05 16:36:04 +02:00
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader;
2017-05-05 16:36:04 +02:00
import fr.gouv.etalab.mastodon.fragments.DisplayAccountsFragment;
import fr.gouv.etalab.mastodon.fragments.DisplayFollowRequestSentFragment;
2017-05-05 16:36:04 +02:00
import fr.gouv.etalab.mastodon.fragments.DisplayNotificationsFragment;
2017-07-16 17:09:35 +02:00
import fr.gouv.etalab.mastodon.fragments.DisplayScheduledTootsFragment;
2017-05-05 16:36:04 +02:00
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnUpdateAccountInfoInterface;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountsAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask;
import fr.gouv.etalab.mastodon.fragments.DisplayStatusFragment;
import fr.gouv.etalab.mastodon.fragments.TabLayoutSettingsFragment;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import mastodon.etalab.gouv.fr.mastodon.R;
import static fr.gouv.etalab.mastodon.helper.Helper.CHANGE_THEME_INTENT;
2017-07-26 18:31:26 +02:00
import static fr.gouv.etalab.mastodon.helper.Helper.CHANGE_USER_INTENT;
import static fr.gouv.etalab.mastodon.helper.Helper.HOME_TIMELINE_INTENT;
2017-05-05 16:36:04 +02:00
import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION;
import static fr.gouv.etalab.mastodon.helper.Helper.NOTIFICATION_INTENT;
import static fr.gouv.etalab.mastodon.helper.Helper.PREF_KEY_ID;
import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor;
import static fr.gouv.etalab.mastodon.helper.Helper.changeUser;
import static fr.gouv.etalab.mastodon.helper.Helper.loadPPInActionBar;
import static fr.gouv.etalab.mastodon.helper.Helper.menuAccounts;
import static fr.gouv.etalab.mastodon.helper.Helper.unCheckAllMenuItems;
import static fr.gouv.etalab.mastodon.helper.Helper.updateHeaderAccountInfo;
import android.support.v4.app.FragmentStatePagerAdapter;
2017-05-05 16:36:04 +02:00
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, OnUpdateAccountInfoInterface {
private FloatingActionButton toot;
private HashMap<String, String> tagTile = new HashMap<>();
private HashMap<String, Integer> tagItem = new HashMap<>();
private TextView toolbarTitle;
private ImageView pp_actionBar;
private SearchView toolbar_search;
private ImageLoader imageLoader;
private DisplayImageOptions options;
private View headerLayout;
public static String currentLocale;
private TabLayout tabLayout;
private ViewPager viewPager;
private RelativeLayout main_app_container;
private Stack<Integer> stackBack = new Stack<>();
2017-08-26 16:27:14 +02:00
private DisplayStatusFragment homeFragment;
private DisplayNotificationsFragment notificationsFragment;
2017-08-22 20:38:25 +02:00
public MainActivity() {
}
2017-05-05 16:36:04 +02:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2017-08-01 15:44:26 +02:00
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
2017-06-30 17:09:07 +02:00
final int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2017-06-30 17:09:07 +02:00
if( theme == Helper.THEME_LIGHT){
setTheme(R.style.AppTheme_NoActionBar);
}else {
setTheme(R.style.AppThemeDark_NoActionBar);
}
2017-05-05 16:36:04 +02:00
setContentView(R.layout.activity_main);
2017-05-05 16:36:04 +02:00
//Test if user is still log in
if( ! Helper.isLoggedIn(getApplicationContext())) {
//It is not, the user is redirected to the login page
Intent myIntent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(myIntent);
finish();
return;
}
Helper.fillMapEmoji(getApplicationContext());
2017-05-05 16:36:04 +02:00
//Here, the user is authenticated
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
2017-05-05 16:36:04 +02:00
setSupportActionBar(toolbar);
toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
pp_actionBar = (ImageView) toolbar.findViewById(R.id.pp_actionBar);
toolbar_search = (SearchView) toolbar.findViewById(R.id.toolbar_search);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
2017-08-24 11:56:29 +02:00
TabLayout.Tab tabHome = tabLayout.newTab();
TabLayout.Tab tabNotif = tabLayout.newTab();
2017-08-24 18:05:57 +02:00
TabLayout.Tab tabLocal = tabLayout.newTab();
TabLayout.Tab tabPublic = tabLayout.newTab();
2017-08-24 11:56:29 +02:00
tabHome.setCustomView(R.layout.tab_badge);
tabNotif.setCustomView(R.layout.tab_badge);
2017-08-24 18:05:57 +02:00
tabLocal.setCustomView(R.layout.tab_badge);
tabPublic.setCustomView(R.layout.tab_badge);
2017-08-24 11:56:29 +02:00
@SuppressWarnings("ConstantConditions") @SuppressLint("CutPasteId")
ImageView iconHome = (ImageView) tabHome.getCustomView().findViewById(R.id.tab_icon);
2017-08-26 16:38:10 +02:00
iconHome.setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.dark_text), PorterDuff.Mode.SRC_IN);
2017-08-24 11:56:29 +02:00
iconHome.setImageResource(R.drawable.ic_action_home_tl);
@SuppressWarnings("ConstantConditions") @SuppressLint("CutPasteId")
ImageView iconNotif = (ImageView) tabNotif.getCustomView().findViewById(R.id.tab_icon);
2017-08-26 16:38:10 +02:00
iconNotif.setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.dark_text), PorterDuff.Mode.SRC_IN);
2017-08-24 11:56:29 +02:00
iconNotif.setImageResource(R.drawable.ic_notifications_tl);
2017-08-24 18:05:57 +02:00
@SuppressWarnings("ConstantConditions") @SuppressLint("CutPasteId")
ImageView iconLocal = (ImageView) tabLocal.getCustomView().findViewById(R.id.tab_icon);
2017-08-26 16:38:10 +02:00
iconLocal.setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.dark_text), PorterDuff.Mode.SRC_IN);
2017-08-24 18:05:57 +02:00
iconLocal.setImageResource(R.drawable.ic_action_users_tl);
@SuppressWarnings("ConstantConditions") @SuppressLint("CutPasteId")
ImageView iconGlobal = (ImageView) tabPublic.getCustomView().findViewById(R.id.tab_icon);
2017-08-26 16:38:10 +02:00
iconGlobal.setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.dark_text), PorterDuff.Mode.SRC_IN);
2017-08-24 18:05:57 +02:00
iconGlobal.setImageResource(R.drawable.ic_action_globe_tl);
2017-08-24 11:56:29 +02:00
changeDrawableColor(getApplicationContext(), R.drawable.ic_action_home_tl,R.color.dark_text);
changeDrawableColor(getApplicationContext(), R.drawable.ic_notifications_tl,R.color.dark_text);
2017-08-24 18:05:57 +02:00
changeDrawableColor(getApplicationContext(), R.drawable.ic_action_users_tl,R.color.dark_text);
changeDrawableColor(getApplicationContext(), R.drawable.ic_action_globe_tl,R.color.dark_text);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
2017-08-24 11:56:29 +02:00
tabLayout.addTab(tabHome);
tabLayout.addTab(tabNotif);
tabLayout.addTab(tabLocal);
tabLayout.addTab(tabPublic);
viewPager = (ViewPager) findViewById(R.id.viewpager);
main_app_container = (RelativeLayout) findViewById(R.id.main_app_container);
PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
2017-08-25 12:53:08 +02:00
final boolean bubbles = sharedpreferences.getBoolean(Helper.SET_BUBBLE_COUNTER, true);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
if (stackBack.empty())
stackBack.push(0);
if (stackBack.contains(tab.getPosition())) {
stackBack.remove(stackBack.indexOf(tab.getPosition()));
stackBack.push(tab.getPosition());
} else {
stackBack.push(tab.getPosition());
}
final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
MenuItem item = null;
String fragmentTag = null;
main_app_container.setVisibility(View.GONE);
viewPager.setVisibility(View.VISIBLE);
Helper.switchLayout(MainActivity.this);
switch (tab.getPosition()){
case 0:
item = navigationView.getMenu().findItem(R.id.nav_home);
2017-07-26 18:31:26 +02:00
fragmentTag = "HOME_TIMELINE";
2017-08-25 12:53:08 +02:00
if( bubbles && homeFragment != null)
2017-08-25 12:45:42 +02:00
homeFragment.refreshData();
2017-08-24 19:28:03 +02:00
updateHomeCounter(0);
break;
case 1:
2017-08-16 10:40:54 +02:00
fragmentTag = "NOTIFICATIONS";
item = navigationView.getMenu().findItem(R.id.nav_notification);
2017-08-24 19:28:03 +02:00
updateNotifCounter(0);
2017-08-25 12:53:08 +02:00
if( bubbles && notificationsFragment != null)
2017-08-25 12:45:42 +02:00
notificationsFragment.refreshData();
2017-08-16 10:40:54 +02:00
break;
case 2:
fragmentTag = "LOCAL_TIMELINE";
item = navigationView.getMenu().findItem(R.id.nav_local);
break;
2017-08-16 10:40:54 +02:00
case 3:
item = navigationView.getMenu().findItem(R.id.nav_global);
fragmentTag = "PUBLIC_TIMELINE";
break;
}
if( item != null){
toolbarTitle.setText(item.getTitle());
populateTitleWithTag(fragmentTag, item.getTitle().toString(), item.getItemId());
2017-07-26 09:21:47 +02:00
unCheckAllMenuItems(navigationView);
item.setChecked(true);
}
2017-08-17 21:24:18 +02:00
if( tab.getPosition() != 1 )
toot.setVisibility(View.VISIBLE);
else
toot.setVisibility(View.GONE);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
if( viewPager.getVisibility() == View.GONE){
viewPager.setVisibility(View.VISIBLE);
Helper.switchLayout(MainActivity.this);
main_app_container.setVisibility(View.GONE);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
2017-08-17 21:24:18 +02:00
if( tab.getPosition() != 1 )
toot.setVisibility(View.VISIBLE);
else
toot.setVisibility(View.GONE);
Fragment fragment = (Fragment) viewPager.getAdapter().instantiateItem(viewPager, tab.getPosition());
switch (tab.getPosition()){
case 0:
case 2:
case 3:
DisplayStatusFragment displayStatusFragment = ((DisplayStatusFragment) fragment);
if( displayStatusFragment != null )
displayStatusFragment.scrollToTop();
break;
case 1:
DisplayNotificationsFragment displayNotificationsFragment = ((DisplayNotificationsFragment) fragment);
if( displayNotificationsFragment != null )
displayNotificationsFragment.scrollToTop();
break;
}
}
});
final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//Scroll to top when top bar is clicked for favourites/blocked/muted
toolbarTitle.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
FragmentManager fragmentManager = getSupportFragmentManager();
if( navigationView.getMenu().findItem(R.id.nav_favorites).isChecked()){
DisplayStatusFragment faveFrag = (DisplayStatusFragment) fragmentManager.findFragmentByTag("FAVOURITES");
if (faveFrag != null && faveFrag.isVisible()) {
faveFrag.scrollToTop();
}
} else if (navigationView.getMenu().findItem(R.id.nav_blocked).isChecked()) {
DisplayAccountsFragment blockFrag = (DisplayAccountsFragment) fragmentManager.findFragmentByTag("BLOCKS");
if (blockFrag != null && blockFrag.isVisible()) {
blockFrag.scrollToTop();
}
} else if (navigationView.getMenu().findItem(R.id.nav_muted).isChecked()) {
DisplayAccountsFragment muteFrag = (DisplayAccountsFragment) fragmentManager.findFragmentByTag("MUTED");
if (muteFrag != null && muteFrag.isVisible()) {
muteFrag.scrollToTop();
}
//Scroll to top when top bar is clicked (THEME_MENU only)
} else if (Helper.THEME_MENU == sharedpreferences.getInt(Helper.SET_TABS, Helper.THEME_TABS)) {
int pos = tabLayout.getSelectedTabPosition();
Fragment fragment = (Fragment) viewPager.getAdapter().instantiateItem(viewPager, pos);
switch (pos) {
case 0:
case 2:
case 3:
DisplayStatusFragment displayStatusFragment = ((DisplayStatusFragment) fragment);
if (displayStatusFragment != null)
displayStatusFragment.scrollToTop();
break;
case 1:
DisplayNotificationsFragment displayNotificationsFragment = ((DisplayNotificationsFragment) fragment);
if (displayNotificationsFragment != null)
displayNotificationsFragment.scrollToTop();
break;
}
}
}
});
toolbar_search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
//Hide keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(toolbar_search.getWindowToken(), 0);
Intent intent = new Intent(MainActivity.this, SearchResultActivity.class);
intent.putExtra("search", query);
startActivity(intent);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
//Hide/Close the searchview
toolbar_search.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
toolbarTitle.setVisibility(View.VISIBLE);
pp_actionBar.setVisibility(View.VISIBLE);
//your code here
return false;
}
});
toolbar_search.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( toolbar_search.isIconified()){
toolbarTitle.setVisibility(View.VISIBLE);
pp_actionBar.setVisibility(View.VISIBLE);
}else {
toolbarTitle.setVisibility(View.GONE);
pp_actionBar.setVisibility(View.GONE);
}
}
});
//Hide the default title
if( getSupportActionBar() != null)
getSupportActionBar().setDisplayShowTitleEnabled(false);
//Defines the current locale of the device in a static variable
currentLocale = Helper.currentLocale(getApplicationContext());
2017-05-05 16:36:04 +02:00
toot = (FloatingActionButton) findViewById(R.id.toot);
toot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), TootActivity.class);
startActivity(intent);
}
});
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
2017-06-11 11:58:46 +02:00
drawer.addDrawerListener(toggle);
2017-05-05 16:36:04 +02:00
toggle.syncState();
2017-05-05 16:36:04 +02:00
//Image loader configuration
imageLoader = ImageLoader.getInstance();
File cacheDir = new File(getCacheDir(), getString(R.string.app_name));
ImageLoaderConfiguration configImg = new ImageLoaderConfiguration.Builder(this)
.imageDownloader(new PatchBaseImageDownloader(getApplicationContext()))
2017-05-05 16:36:04 +02:00
.threadPoolSize(5)
.threadPriority(Thread.MIN_PRIORITY + 3)
.denyCacheImageMultipleSizesInMemory()
.diskCache(new UnlimitedDiskCache(cacheDir))
.build();
imageLoader.init(configImg);
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
options = new DisplayImageOptions.Builder().displayer(new RoundedBitmapDisplayer(90)).cacheInMemory(false)
2017-05-05 16:36:04 +02:00
.cacheOnDisk(true).resetViewBeforeLoading(true).build();
headerLayout = navigationView.getHeaderView(0);
2017-05-05 16:36:04 +02:00
String prefKeyOauthTokenT = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
Account account = new AccountDAO(getApplicationContext(), db).getAccountByToken(prefKeyOauthTokenT);
updateHeaderAccountInfo(MainActivity.this, account, headerLayout, imageLoader, options);
loadPPInActionBar(MainActivity.this, account.getAvatar());
2017-06-07 13:58:21 +02:00
//Locked account can see follow request
if (account.isLocked()) {
navigationView.getMenu().findItem(R.id.nav_follow_request).setVisible(true);
} else {
navigationView.getMenu().findItem(R.id.nav_follow_request).setVisible(false);
}
LinearLayout owner_container = (LinearLayout) headerLayout.findViewById(R.id.owner_container);
owner_container.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
menuAccounts(MainActivity.this);
}
});
boolean matchingIntent = mamageNewIntent(getIntent());
if (savedInstanceState == null && !matchingIntent) {
2017-05-05 16:36:04 +02:00
navigationView.setCheckedItem(R.id.nav_home);
navigationView.getMenu().performIdentifierAction(R.id.nav_home, 0);
toolbarTitle.setText(R.string.home_menu);
2017-05-05 16:36:04 +02:00
}
2017-08-01 15:44:26 +02:00
boolean popupShown = sharedpreferences.getBoolean(Helper.SET_POPUP_PUSH, false);
if(!popupShown){
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = getLayoutInflater();
2017-08-26 10:57:08 +02:00
@SuppressLint("InflateParams") View dialogView = inflater.inflate(R.layout.popup_quick_settings, null);
2017-08-01 15:44:26 +02:00
dialogBuilder.setView(dialogView);
final SwitchCompat set_push_hometimeline = (SwitchCompat) dialogView.findViewById(R.id.set_push_hometimeline);
final SwitchCompat set_push_notification = (SwitchCompat) dialogView.findViewById(R.id.set_push_notification);
boolean notif_hometimeline = sharedpreferences.getBoolean(Helper.SET_NOTIF_HOMETIMELINE, true);
boolean notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true);
boolean notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD, true);
boolean notif_ask = sharedpreferences.getBoolean(Helper.SET_NOTIF_ASK, true);
boolean notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION, true);
boolean notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE, true);
boolean notifif_notifications = !( !notif_follow && !notif_add && !notif_ask && !notif_mention && !notif_share);
set_push_hometimeline.setChecked(notif_hometimeline);
set_push_notification.setChecked(notifif_notifications);
dialogBuilder.setTitle(R.string.settings_popup_title);
dialogBuilder.setCancelable(false);
dialogBuilder.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean(Helper.SET_NOTIF_FOLLOW, set_push_notification.isChecked());
editor.putBoolean(Helper.SET_NOTIF_ADD, set_push_notification.isChecked());
editor.putBoolean(Helper.SET_NOTIF_ASK, set_push_notification.isChecked());
editor.putBoolean(Helper.SET_NOTIF_MENTION, set_push_notification.isChecked());
editor.putBoolean(Helper.SET_NOTIF_SHARE, set_push_notification.isChecked());
editor.putBoolean(Helper.SET_NOTIF_HOMETIMELINE, set_push_hometimeline.isChecked());
editor.putBoolean(Helper.SET_POPUP_PUSH, true);
editor.apply();
}
}).show();
}
2017-08-12 09:53:07 +02:00
Helper.switchLayout(MainActivity.this);
2017-08-22 20:38:25 +02:00
2017-05-05 16:36:04 +02:00
}
2017-08-22 20:38:25 +02:00
2017-05-05 16:36:04 +02:00
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
mamageNewIntent(intent);
}
/**
* Manages new intents
* @param intent Intent - intent related to a notification in top bar
*/
private boolean mamageNewIntent(Intent intent){
2017-05-05 16:36:04 +02:00
if( intent == null || intent.getExtras() == null )
return false;
2017-06-30 17:09:07 +02:00
String action = intent.getAction();
String type = intent.getType();
2017-05-05 16:36:04 +02:00
Bundle extras = intent.getExtras();
String userIdIntent;
boolean matchingIntent = false;
2017-05-05 16:36:04 +02:00
if( extras.containsKey(INTENT_ACTION) ){
final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
userIdIntent = extras.getString(PREF_KEY_ID); //Id of the account in the intent
if (extras.getInt(INTENT_ACTION) == NOTIFICATION_INTENT){
changeUser(MainActivity.this, userIdIntent, false); //Connects the account which is related to the notification
2017-07-26 09:21:47 +02:00
unCheckAllMenuItems(navigationView);
2017-08-26 10:57:08 +02:00
if( tabLayout.getTabAt(1) != null)
//noinspection ConstantConditions
tabLayout.getTabAt(1).select();
matchingIntent = true;
}else if( extras.getInt(INTENT_ACTION) == HOME_TIMELINE_INTENT){
2017-07-26 18:31:26 +02:00
changeUser(MainActivity.this, userIdIntent, true); //Connects the account which is related to the notification
}else if( extras.getInt(INTENT_ACTION) == CHANGE_THEME_INTENT){
2017-07-26 09:21:47 +02:00
unCheckAllMenuItems(navigationView);
navigationView.setCheckedItem(R.id.nav_settings);
navigationView.getMenu().performIdentifierAction(R.id.nav_settings, 0);
toolbarTitle.setText(R.string.settings);
matchingIntent = true;
2017-07-26 18:31:26 +02:00
}else if( extras.getInt(INTENT_ACTION) == CHANGE_USER_INTENT){
unCheckAllMenuItems(navigationView);
navigationView.setCheckedItem(R.id.nav_home);
navigationView.getMenu().performIdentifierAction(R.id.nav_home, 0);
toolbarTitle.setText(R.string.home_menu);
matchingIntent = true;
2017-05-05 16:36:04 +02:00
}
}else if( Intent.ACTION_SEND.equals(action) && type != null ) {
if ("text/plain".equals(type)) {
String sharedSubject = intent.getStringExtra(Intent.EXTRA_SUBJECT);
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
Intent intentToot = new Intent(getApplicationContext(), TootActivity.class);
Bundle b = new Bundle();
b.putString("sharedSubject", sharedSubject);
b.putString("sharedContent", sharedText);
intentToot.putExtras(b);
startActivity(intentToot);
}
} else if (type.startsWith("image/")) {
2017-08-26 10:57:08 +02:00
Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
Intent intentToot = new Intent(getApplicationContext(), TootActivity.class);
Bundle b = new Bundle();
b.putParcelable("sharedUri", imageUri);
2017-08-22 11:59:25 +02:00
b.putInt("uriNumber", 1);
intentToot.putExtras(b);
startActivity(intentToot);
}
}
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null ) {
if (type.startsWith("image/")) {
ArrayList<Uri> imageList = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (imageList != null) {
Intent intentToot = new Intent(getApplicationContext(), TootActivity.class);
Bundle b = new Bundle();
b.putParcelableArrayList("sharedUri", imageList);
2017-08-22 11:59:25 +02:00
b.putInt("uriNumber", imageList.size());
intentToot.putExtras(b);
startActivity(intentToot);
}
}
2017-05-05 16:36:04 +02:00
}
intent.replaceExtras(new Bundle());
intent.setAction("");
intent.setData(null);
intent.setFlags(0);
return matchingIntent;
2017-05-05 16:36:04 +02:00
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
//Hide search bar on back pressed
if( !toolbar_search.isIconified()){
toolbar_search.setIconified(true);
}
if( viewPager.getVisibility() == View.VISIBLE){
if (stackBack.size() > 1) {
stackBack.pop();
viewPager.setCurrentItem(stackBack.lastElement());
}else {
super.onBackPressed();
}
}else {
viewPager.setVisibility(View.VISIBLE);
Helper.switchLayout(MainActivity.this);
main_app_container.setVisibility(View.GONE);
final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
2017-07-26 09:21:47 +02:00
unCheckAllMenuItems(navigationView);
2017-08-26 18:24:46 +02:00
toot.setVisibility(View.VISIBLE);
//Manages theme for icon colors
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
if( theme == Helper.THEME_DARK){
changeDrawableColor(getApplicationContext(), R.drawable.ic_reply,R.color.dark_text);
changeDrawableColor(getApplicationContext(), R.drawable.ic_action_more,R.color.dark_text);
changeDrawableColor(getApplicationContext(), R.drawable.ic_action_globe,R.color.dark_text);
changeDrawableColor(getApplicationContext(), R.drawable.ic_action_lock_open,R.color.dark_text);
changeDrawableColor(getApplicationContext(), R.drawable.ic_action_lock_closed,R.color.dark_text);
changeDrawableColor(getApplicationContext(), R.drawable.ic_local_post_office,R.color.dark_text);
changeDrawableColor(getApplicationContext(), R.drawable.ic_retweet_black,R.color.dark_text);
changeDrawableColor(getApplicationContext(), R.drawable.ic_fav_black,R.color.dark_text);
changeDrawableColor(getApplicationContext(), R.drawable.ic_photo,R.color.dark_text);
changeDrawableColor(getApplicationContext(), R.drawable.ic_remove_red_eye,R.color.dark_text);
changeDrawableColor(getApplicationContext(), R.drawable.ic_translate,R.color.dark_text);
}else {
changeDrawableColor(getApplicationContext(), R.drawable.ic_reply,R.color.black);
changeDrawableColor(getApplicationContext(), R.drawable.ic_action_more,R.color.black);
changeDrawableColor(getApplicationContext(), R.drawable.ic_action_globe,R.color.black);
changeDrawableColor(getApplicationContext(), R.drawable.ic_action_lock_open,R.color.black);
changeDrawableColor(getApplicationContext(), R.drawable.ic_action_lock_closed,R.color.black);
changeDrawableColor(getApplicationContext(), R.drawable.ic_local_post_office,R.color.black);
changeDrawableColor(getApplicationContext(), R.drawable.ic_retweet_black,R.color.black);
changeDrawableColor(getApplicationContext(), R.drawable.ic_fav_black,R.color.black);
changeDrawableColor(getApplicationContext(), R.drawable.ic_photo,R.color.white);
changeDrawableColor(getApplicationContext(), R.drawable.ic_remove_red_eye,R.color.white);
changeDrawableColor(getApplicationContext(), R.drawable.ic_translate,R.color.white);
}
switch (viewPager.getCurrentItem()){
case 0:
toolbarTitle.setText(R.string.home_menu);
navigationView.getMenu().findItem(R.id.nav_home).setChecked(true);
break;
case 1:
2017-08-17 21:24:18 +02:00
toot.setVisibility(View.GONE);
2017-08-16 10:40:54 +02:00
toolbarTitle.setText(R.string.notifications);
navigationView.getMenu().findItem(R.id.nav_notification).setChecked(true);
break;
case 2:
2017-08-16 10:40:54 +02:00
toolbarTitle.setText(R.string.local_menu);
navigationView.getMenu().findItem(R.id.nav_local).setChecked(true);
break;
case 3:
2017-08-26 16:27:14 +02:00
toolbarTitle.setText(R.string.global_menu);
2017-08-16 10:40:54 +02:00
navigationView.getMenu().findItem(R.id.nav_global).setChecked(true);
break;
}
}
2017-05-05 16:36:04 +02:00
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if(id == R.id.action_logout) {
Helper.logout(getApplicationContext());
Intent myIntent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(myIntent);
finish();
return true;
}else if(id == R.id.action_about){
Intent intent = new Intent(getApplicationContext(), AboutActivity.class);
startActivity(intent);
}else if(id == R.id.action_privacy){
Intent intent = new Intent(getApplicationContext(), PrivacyActivity.class);
startActivity(intent);
}else if(id == R.id.action_about_instance){
Intent intent = new Intent(getApplicationContext(), InstanceActivity.class);
startActivity(intent);
2017-06-19 19:20:20 +02:00
} else if( id == R.id.action_cache){ //Cache clear feature
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(R.string.cache_title);
long sizeCache = Helper.cacheSize(getCacheDir());
float cacheSize = 0;
if( sizeCache > 0 ) {
if (sizeCache > 0) {
cacheSize = (float) sizeCache / 1000000.0f;
}
}
final float finalCacheSize = cacheSize;
builder.setMessage(getString(R.string.cache_message, String.format("%s %s", String.format(Locale.getDefault(), "%.2f", cacheSize), getString(R.string.cache_units))))
2017-06-19 19:20:20 +02:00
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
try {
String path = getCacheDir().getPath();
File dir = new File(path);
if (dir.isDirectory()) {
Helper.deleteDir(dir);
}
} catch (Exception ignored) {}
Toast.makeText(MainActivity.this, getString(R.string.toast_cache_clear,String.format("%s %s", String.format(Locale.getDefault(), "%.2f", finalCacheSize), getString(R.string.cache_units))), Toast.LENGTH_LONG).show();
2017-06-19 19:20:20 +02:00
dialog.dismiss();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}else if( id == R.id.action_size){
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
int textSize = sharedpreferences.getInt(Helper.SET_TEXT_SIZE,110);
int iconSize = sharedpreferences.getInt(Helper.SET_ICON_SIZE,130);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(R.string.text_size);
2017-08-26 10:57:08 +02:00
@SuppressLint("InflateParams") View popup_quick_settings = getLayoutInflater().inflate( R.layout.popup_text_size, null );
builder.setView(popup_quick_settings);
SeekBar set_text_size = (SeekBar) popup_quick_settings.findViewById(R.id.set_text_size);
SeekBar set_icon_size = (SeekBar) popup_quick_settings.findViewById(R.id.set_icon_size);
final TextView set_text_size_value = (TextView) popup_quick_settings.findViewById(R.id.set_text_size_value);
final TextView set_icon_size_value = (TextView) popup_quick_settings.findViewById(R.id.set_icon_size_value);
set_text_size_value.setText(String.format("%s%%",String.valueOf(textSize)));
set_icon_size_value.setText(String.format("%s%%",String.valueOf(iconSize)));
2017-08-15 12:12:51 +02:00
set_text_size.setMax(20);
set_icon_size.setMax(20);
2017-08-18 09:11:38 +02:00
set_text_size.setProgress(((textSize-80)/5));
set_icon_size.setProgress(((iconSize-80)/5));
set_text_size.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
2017-08-18 09:11:38 +02:00
int value = 80 + progress*5;
set_text_size_value.setText(String.format("%s%%",String.valueOf(value)));
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putInt(Helper.SET_TEXT_SIZE, value);
editor.apply();
}
});
set_icon_size.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
2017-08-18 09:11:38 +02:00
int value = 80 + progress*5;
set_icon_size_value.setText(String.format("%s%%",String.valueOf(value)));
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putInt(Helper.SET_ICON_SIZE, value);
editor.apply();
}
});
builder.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
2017-08-15 12:12:51 +02:00
MainActivity.this.recreate();
dialog.dismiss();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
2017-05-05 16:36:04 +02:00
}
return super.onOptionsItemSelected(item);
}
@Override
public void onResume(){
super.onResume();
2017-08-25 12:53:08 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean bubbles = sharedpreferences.getBoolean(Helper.SET_BUBBLE_COUNTER, true);
if( bubbles){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {refreshData();}
2017-08-25 15:30:35 +02:00
}, 1000);
2017-08-25 12:53:08 +02:00
}
2017-05-05 16:36:04 +02:00
//Proceeds to update of the authenticated account
if(Helper.isLoggedIn(getApplicationContext()))
new UpdateAccountInfoByIDAsyncTask(getApplicationContext(), MainActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2017-05-05 16:36:04 +02:00
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
2017-08-22 17:34:26 +02:00
if( id == R.id.nav_remote_follow){
Intent remoteFollow = new Intent(getApplicationContext(), RemoteFollowActivity.class);
startActivity(remoteFollow);
return false;
}
2017-07-21 21:09:32 +02:00
final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
2017-07-26 09:21:47 +02:00
unCheckAllMenuItems(navigationView);
2017-07-21 21:09:32 +02:00
item.setChecked(true);
2017-05-05 16:36:04 +02:00
//Remove the search bar
if( !toolbar_search.isIconified() ) {
toolbarTitle.setVisibility(View.VISIBLE);
pp_actionBar.setVisibility(View.VISIBLE);
toolbar_search.setIconified(true);
}
toolbarTitle.setText(item.getTitle());
if (id == R.id.nav_home) {
//noinspection ConstantConditions
tabLayout.getTabAt(0).select();
return true;
2017-08-16 10:40:54 +02:00
} else if( id == R.id.nav_notification){
//noinspection ConstantConditions
tabLayout.getTabAt(1).select();
return true;
2017-08-16 10:40:54 +02:00
}else if (id == R.id.nav_local) {
//noinspection ConstantConditions
tabLayout.getTabAt(2).select();
return true;
2017-08-16 10:40:54 +02:00
} else if (id == R.id.nav_global) {
//noinspection ConstantConditions
tabLayout.getTabAt(3).select();
return true;
2017-05-05 16:36:04 +02:00
}
DisplayStatusFragment statusFragment;
DisplayAccountsFragment accountsFragment;
Bundle bundle = new Bundle();
FragmentManager fragmentManager = getSupportFragmentManager();
String fragmentTag = null;
main_app_container.setVisibility(View.VISIBLE);
viewPager.setVisibility(View.GONE);
tabLayout.setVisibility(View.GONE);
if (id == R.id.nav_settings) {
2017-05-05 16:36:04 +02:00
toot.setVisibility(View.GONE);
TabLayoutSettingsFragment tabLayoutSettingsFragment= new TabLayoutSettingsFragment();
fragmentTag = "TABLAYOUT_SETTINGS";
fragmentManager.beginTransaction()
.replace(R.id.main_app_container, tabLayoutSettingsFragment, fragmentTag).commit();
2017-05-05 16:36:04 +02:00
} else if (id == R.id.nav_favorites) {
toot.setVisibility(View.GONE);
statusFragment = new DisplayStatusFragment();
bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.FAVOURITES);
statusFragment.setArguments(bundle);
fragmentTag = "FAVOURITES";
fragmentManager.beginTransaction()
.replace(R.id.main_app_container, statusFragment, fragmentTag).commit();
2017-05-05 16:36:04 +02:00
} else if (id == R.id.nav_blocked) {
toot.setVisibility(View.GONE);
accountsFragment = new DisplayAccountsFragment();
bundle.putSerializable("type", RetrieveAccountsAsyncTask.Type.BLOCKED);
accountsFragment.setArguments(bundle);
fragmentTag = "BLOCKS";
fragmentManager.beginTransaction()
.replace(R.id.main_app_container, accountsFragment, fragmentTag).commit();
2017-05-05 16:36:04 +02:00
}else if (id == R.id.nav_muted) {
toot.setVisibility(View.GONE);
accountsFragment = new DisplayAccountsFragment();
bundle.putSerializable("type", RetrieveAccountsAsyncTask.Type.MUTED);
accountsFragment.setArguments(bundle);
fragmentTag = "MUTED";
2017-05-05 16:36:04 +02:00
fragmentManager.beginTransaction()
.replace(R.id.main_app_container, accountsFragment, fragmentTag).commit();
2017-07-16 17:09:35 +02:00
}else if (id == R.id.nav_scheduled) {
toot.setVisibility(View.VISIBLE);
2017-07-16 17:09:35 +02:00
DisplayScheduledTootsFragment displayScheduledTootsFragment = new DisplayScheduledTootsFragment();
fragmentTag = "SCHEDULED";
fragmentManager.beginTransaction()
.replace(R.id.main_app_container, displayScheduledTootsFragment, fragmentTag).commit();
}else if( id == R.id.nav_follow_request){
toot.setVisibility(View.GONE);
DisplayFollowRequestSentFragment followRequestSentFragment = new DisplayFollowRequestSentFragment();
fragmentTag = "FOLLOW_REQUEST_SENT";
fragmentManager.beginTransaction()
.replace(R.id.main_app_container, followRequestSentFragment, fragmentTag).commit();
2017-05-05 16:36:04 +02:00
}
2017-05-05 16:36:04 +02:00
populateTitleWithTag(fragmentTag, item.getTitle().toString(), item.getItemId());
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
2017-05-05 16:36:04 +02:00
private void populateTitleWithTag(String tag, String title, int index){
if( tag == null)
return;
if ( tagTile.get(tag) == null)
tagTile.put(tag, title);
if ( tagItem.get(tag) == null)
tagItem.put(tag, index);
}
@Override
public void setTitle(CharSequence title) {
if(toolbarTitle != null )
toolbarTitle.setText(title);
2017-05-05 16:36:04 +02:00
}
@Override
public void onUpdateAccountInfo(boolean error) {
if( error){
//An error occurred, the user is redirected to the login page
2017-05-05 16:36:04 +02:00
Helper.logout(getApplicationContext());
Intent myIntent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(myIntent);
finish();
}else {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
SQLiteDatabase db = Sqlite.getInstance(MainActivity.this, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Account account = new AccountDAO(getApplicationContext(), db).getAccountByID(userId);
updateHeaderAccountInfo(MainActivity.this, account, headerLayout, imageLoader, options);
2017-05-05 16:36:04 +02:00
}
}
2017-08-26 16:27:14 +02:00
2017-06-11 11:58:46 +02:00
/**
* Page Adapter for settings
2017-06-11 11:58:46 +02:00
*/
2017-07-26 18:31:26 +02:00
private class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
private PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
//Remove the search bar
if( !toolbar_search.isIconified() ) {
toolbarTitle.setVisibility(View.VISIBLE);
pp_actionBar.setVisibility(View.VISIBLE);
toolbar_search.setIconified(true);
}
//Selection comes from another menu, no action to do
DisplayStatusFragment statusFragment;
Bundle bundle = new Bundle();
switch (position) {
case 0:
homeFragment = new DisplayStatusFragment();
bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.HOME);
homeFragment.setArguments(bundle);
return homeFragment;
case 1:
notificationsFragment = new DisplayNotificationsFragment();
return notificationsFragment;
2017-08-16 10:40:54 +02:00
case 2:
statusFragment = new DisplayStatusFragment();
bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.LOCAL);
statusFragment.setArguments(bundle);
return statusFragment;
2017-08-16 10:40:54 +02:00
case 3:
statusFragment = new DisplayStatusFragment();
bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.PUBLIC);
statusFragment.setArguments(bundle);
return statusFragment;
2017-08-26 16:27:14 +02:00
}
return null;
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
private void refreshData(){
2017-08-24 13:03:22 +02:00
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
String prefKeyOauthTokenT = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Account account = new AccountDAO(getApplicationContext(), db).getAccountByToken(prefKeyOauthTokenT);
if( account != null){
2017-08-25 10:25:13 +02:00
String last_refresh = sharedpreferences.getString(Helper.LAST_BUBBLE_REFRESH_NOTIF + account.getId(), null);
2017-08-24 13:03:22 +02:00
Date last_refresh_date = Helper.stringToDate(getApplicationContext(), last_refresh);
2017-08-26 15:51:03 +02:00
if (last_refresh_date == null || (new Date().getTime() - last_refresh_date.getTime()) >= TimeUnit.SECONDS.toMillis(60)) {
2017-08-25 10:25:13 +02:00
2017-08-25 15:30:35 +02:00
if( notificationsFragment != null ){
2017-08-24 15:57:35 +02:00
notificationsFragment.update();
SharedPreferences.Editor editor = sharedpreferences.edit();
2017-08-25 10:25:13 +02:00
editor.putString(Helper.LAST_BUBBLE_REFRESH_NOTIF+ account.getId(),Helper.dateToString(getApplicationContext(), new Date()));
2017-08-24 15:57:35 +02:00
editor.apply();
}
2017-08-24 13:03:22 +02:00
}
2017-08-25 10:25:13 +02:00
last_refresh = sharedpreferences.getString(Helper.LAST_BUBBLE_REFRESH_HOME + account.getId(), null);
last_refresh_date = Helper.stringToDate(getApplicationContext(), last_refresh);
2017-08-26 15:51:03 +02:00
if (last_refresh_date == null || (new Date().getTime() - last_refresh_date.getTime()) >= TimeUnit.SECONDS.toMillis(60)) {
2017-08-25 15:30:35 +02:00
if( homeFragment != null ){
2017-08-25 10:25:13 +02:00
homeFragment.update();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_BUBBLE_REFRESH_HOME+ account.getId(),Helper.dateToString(getApplicationContext(), new Date()));
editor.apply();
}
}
2017-08-24 13:03:22 +02:00
}
}
public void updateHomeCounter(int newHomeCount){
if( tabLayout.getTabAt(0) == null )
return;
2017-08-26 10:57:08 +02:00
//noinspection ConstantConditions
View tabHome = tabLayout.getTabAt(0).getCustomView();
2017-08-26 10:57:08 +02:00
if( tabHome == null)
return;
TextView tabCounterHome = (TextView) tabHome.findViewById(R.id.tab_counter);
2017-08-24 17:35:39 +02:00
tabCounterHome.setText(String.valueOf(newHomeCount));
if( newHomeCount > 0){
//New data are available
//The fragment is not displayed, so the counter is displayed
if( tabLayout.getSelectedTabPosition() != 0)
tabCounterHome.setVisibility(View.VISIBLE);
else
tabCounterHome.setVisibility(View.GONE);
}else {
tabCounterHome.setVisibility(View.GONE);
}
}
public void updateNotifCounter(int newNotifCount){
if(tabLayout.getTabAt(1) == null)
return;
2017-08-26 10:57:08 +02:00
//noinspection ConstantConditions
View tabNotif = tabLayout.getTabAt(1).getCustomView();
2017-08-26 10:57:08 +02:00
if( tabNotif == null)
return;
TextView tabCounterNotif = (TextView) tabNotif.findViewById(R.id.tab_counter);
2017-08-24 17:35:39 +02:00
tabCounterNotif.setText(String.valueOf(newNotifCount));
if( newNotifCount > 0){
if( tabLayout.getSelectedTabPosition() != 1)
tabCounterNotif.setVisibility(View.VISIBLE);
else
tabCounterNotif.setVisibility(View.GONE);
}else {
tabCounterNotif.setVisibility(View.GONE);
}
}
2017-05-05 16:36:04 +02:00
}