Merged in develop (pull request #3)

This commit is contained in:
tom79 2017-05-17 14:29:38 +00:00
commit f4749833da
5 changed files with 121 additions and 48 deletions

View File

@ -7,8 +7,8 @@ android {
applicationId "fr.gouv.etalab.mastodon"
minSdkVersion 15
targetSdkVersion 25
versionCode 4
versionName "1.0.4"
versionCode 5
versionName "1.0.5"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {

View File

@ -23,7 +23,6 @@ import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
@ -49,6 +48,7 @@ import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import java.io.File;
import java.util.HashMap;
import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoByIDAsyncTask;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.fragments.DisplayAccountsFragment;
import fr.gouv.etalab.mastodon.fragments.DisplayNotificationsFragment;
@ -57,7 +57,6 @@ 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.asynctasks.UpdateAccountInfoAsyncTask;
import fr.gouv.etalab.mastodon.fragments.DisplayStatusFragment;
import fr.gouv.etalab.mastodon.fragments.TabLayoutSettingsFragment;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
@ -74,6 +73,9 @@ public class MainActivity extends AppCompatActivity
private HashMap<String, String> tagTile = new HashMap<>();
private HashMap<String, Integer> tagItem = new HashMap<>();
private Toolbar toolbar;
private ImageLoader imageLoader;
private DisplayImageOptions options;
private View headerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -113,7 +115,6 @@ public class MainActivity extends AppCompatActivity
//Image loader configuration
ImageLoader imageLoader;
imageLoader = ImageLoader.getInstance();
File cacheDir = new File(getCacheDir(), getString(R.string.app_name));
ImageLoaderConfiguration configImg = new ImageLoaderConfiguration.Builder(this)
@ -124,38 +125,16 @@ public class MainActivity extends AppCompatActivity
.build();
imageLoader.init(configImg);
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
DisplayImageOptions options = new DisplayImageOptions.Builder().displayer(new RoundedBitmapDisplayer(90)).cacheInMemory(false)
options = new DisplayImageOptions.Builder().displayer(new RoundedBitmapDisplayer(90)).cacheInMemory(false)
.cacheOnDisk(true).resetViewBeforeLoading(true).build();
View headerLayout = navigationView.getHeaderView(0);
headerLayout = navigationView.getHeaderView(0);
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String prefKeyOauthTokenT = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
ImageView profilePicture = (ImageView) headerLayout.findViewById(R.id.profilePicture);
TextView username = (TextView) headerLayout.findViewById(R.id.username);
TextView displayedName = (TextView) headerLayout.findViewById(R.id.displayedName);
TextView ownerStatus = (TextView) headerLayout.findViewById(R.id.owner_status);
TextView ownerFollowing = (TextView) headerLayout.findViewById(R.id.owner_following);
TextView ownerFollowers = (TextView) headerLayout.findViewById(R.id.owner_followers);
Account account = new AccountDAO(getApplicationContext(), db).getAccountByToken(prefKeyOauthTokenT);
//Something wrong happened with the account recorded in db (ie: bad token)
if( account == null ) {
Helper.logout(getApplicationContext());
Intent myIntent = new Intent(MainActivity.this, LoginActivity.class);
Toast.makeText(getApplicationContext(),R.string.toast_error, Toast.LENGTH_LONG).show();
startActivity(myIntent);
finish(); //User is logged out to get a new token
}else {
ownerStatus.setText(String.valueOf(account.getStatuses_count()));
ownerFollowers.setText(String.valueOf(account.getFollowers_count()));
ownerFollowing.setText(String.valueOf(account.getFollowing_count()));
username.setText(String.format("@%s",account.getUsername()));
displayedName.setText(account.getDisplay_name());
imageLoader.displayImage(account.getAvatar(), profilePicture, options);
}
updateHeaderAccountInfo(account);
boolean menuWasSelected = false;
if( getIntent() != null && getIntent().getExtras() != null ){
Bundle extras = getIntent().getExtras();
@ -325,7 +304,7 @@ public class MainActivity extends AppCompatActivity
super.onResume();
//Proceeds to update of the authenticated account
if(Helper.isLoggedIn(getApplicationContext()))
new UpdateAccountInfoAsyncTask(getApplicationContext(), null, MainActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new UpdateAccountInfoByIDAsyncTask(getApplicationContext(), MainActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@ -430,6 +409,30 @@ public class MainActivity extends AppCompatActivity
return true;
}
private void updateHeaderAccountInfo(Account account){
ImageView profilePicture = (ImageView) headerLayout.findViewById(R.id.profilePicture);
TextView username = (TextView) headerLayout.findViewById(R.id.username);
TextView displayedName = (TextView) headerLayout.findViewById(R.id.displayedName);
TextView ownerStatus = (TextView) headerLayout.findViewById(R.id.owner_status);
TextView ownerFollowing = (TextView) headerLayout.findViewById(R.id.owner_following);
TextView ownerFollowers = (TextView) headerLayout.findViewById(R.id.owner_followers);
//Something wrong happened with the account recorded in db (ie: bad token)
if( account == null ) {
Helper.logout(getApplicationContext());
Intent myIntent = new Intent(MainActivity.this, LoginActivity.class);
Toast.makeText(getApplicationContext(),R.string.toast_error, Toast.LENGTH_LONG).show();
startActivity(myIntent);
finish(); //User is logged out to get a new token
}else {
ownerStatus.setText(String.valueOf(account.getStatuses_count()));
ownerFollowers.setText(String.valueOf(account.getFollowers_count()));
ownerFollowing.setText(String.valueOf(account.getFollowing_count()));
username.setText(String.format("@%s",account.getUsername()));
displayedName.setText(account.getDisplay_name());
imageLoader.displayImage(account.getAvatar(), profilePicture, options);
}
}
private void populateTitleWithTag(String tag, String title, int index){
if( tag == null)
return;
@ -448,11 +451,17 @@ public class MainActivity extends AppCompatActivity
@Override
public void onUpdateAccountInfo(boolean error) {
if( error){
//It is not, the user is redirected to the login page
//An error occurred, the user is redirected to the login page
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(account);
}
}
}

View File

@ -0,0 +1,67 @@
/* Copyright 2017 Thomas Schneider
*
* This file is a part of Mastodon Etalab for mastodon.etalab.gouv.fr
*
* 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.
*
* Mastodon Etalab 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 Thomas Schneider; if not,
* see <http://www.gnu.org/licenses>. */
package fr.gouv.etalab.mastodon.asynctasks;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnUpdateAccountInfoInterface;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
/**
* Created by Thomas on 17/05/2017.
* Manage the synchronization with the authenticated account and update the db not
*/
public class UpdateAccountInfoByIDAsyncTask extends AsyncTask<Void, Void, Void> {
private Context context;
private OnUpdateAccountInfoInterface listener;
public UpdateAccountInfoByIDAsyncTask(Context context, OnUpdateAccountInfoInterface onUpdateAccountInfoInterface){
this.context = context;
this.listener = onUpdateAccountInfoInterface;
}
@Override
protected Void doInBackground(Void... params) {
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
Account account = new API(context).getAccount(userId);
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
boolean userExists = new AccountDAO(context, db).userExist(account);
if( userExists) {
Account accountDb = new AccountDAO(context, db).getAccountByID(userId);
if( accountDb != null){
account.setInstance(accountDb.getInstance());
account.setToken(accountDb.getToken());
new AccountDAO(context, db).updateAccount(account);
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
listener.onUpdateAccountInfo(false);
}
}

View File

@ -173,20 +173,16 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf
}
//Click on a conversation
if( type != RetrieveFeedsAsyncTask.Type.CONTEXT ){
if( !status.getIn_reply_to_account_id().equals("null") || !status.getIn_reply_to_id().equals("null") ) {
holder.status_content.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, ShowConversationActivity.class);
Bundle b = new Bundle();
b.putString("statusId", status.getId()); //Your id
intent.putExtras(b); //Put your id to your next Intent
context.startActivity(intent);
}
});
}else{
holder.status_content.setOnClickListener(null);
}
holder.status_content.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, ShowConversationActivity.class);
Bundle b = new Bundle();
b.putString("statusId", status.getId()); //Your id
intent.putExtras(b); //Put your id to your next Intent
context.startActivity(intent);
}
});
}else {
if( position == ShowConversationActivity.position){
holder.main_container.setBackgroundResource(R.color.blue_light);
@ -410,6 +406,8 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf
String url = attachment.getPreview_url();
if( url == null || url.trim().equals(""))
url = attachment.getUrl();
if( url.trim().equals("https://mastodon.etalab.gouv.fr/files/small/missing.png"))
continue;
imageLoader.displayImage(url, imageView, options);
imageView.setOnClickListener(new View.OnClickListener() {
@Override

View File

@ -27,7 +27,6 @@ import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;
@ -123,7 +122,7 @@ public class Helper {
* Check if the user is connected to Internet
* @return boolean
*/
public static boolean isConnectingToInternet(Context context) {
public static boolean isConnectedToInternet(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if ( ni != null && ni.isConnected()) {