Cleanup, bugfix

This commit is contained in:
Sufian 2018-05-19 11:36:31 +02:00
parent fff724d2f4
commit 069fd694c3
10 changed files with 62 additions and 51 deletions

View File

@ -1,5 +1,6 @@
package org.nuclearfog.twidda.backend;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
@ -18,24 +19,22 @@ import org.nuclearfog.twidda.R;
import org.nuclearfog.twidda.database.ErrorLog;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.URL;
public class ImagePopup extends AsyncTask<String, Void, Boolean> {
private ImageView mImg;
private WeakReference<Context> ui;
private Dialog popup;
private Bitmap imgArray[];
private ProgressBar mCircle;
private Context c;
private LayoutInflater inf;
private int index = 0;
private int position = 0;
public ImagePopup(Context c) {
popup = new Dialog(c);
mCircle = new ProgressBar(c);
ui = new WeakReference<>(c);
inf = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.c = c;
}
@Override
@ -43,6 +42,7 @@ public class ImagePopup extends AsyncTask<String, Void, Boolean> {
popup.requestWindowFeature(Window.FEATURE_NO_TITLE);
if(popup.getWindow() != null)
popup.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
ProgressBar mCircle = new ProgressBar(ui.get());
popup.setContentView(mCircle);
popup.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
@ -76,18 +76,19 @@ public class ImagePopup extends AsyncTask<String, Void, Boolean> {
} catch (Exception err) {
Log.e("shitter:","Image download failed!");
err.printStackTrace();
ErrorLog errorLog = new ErrorLog(c);
ErrorLog errorLog = new ErrorLog(ui.get());
errorLog.add(err.getMessage());
return false;
}
}
@Override
@SuppressLint("InflateParams")
protected void onPostExecute(Boolean result) {
if(result) {
View content = inf.inflate(R.layout.imagepreview,null);
mImg = content.findViewById(R.id.fullSizeImage);
setImage(imgArray[position]);
View content = inf.inflate(R.layout.imagepreview,null,false);
ImageView mImg = content.findViewById(R.id.fullSizeImage);
setImage(imgArray[position], mImg);
popup.setContentView(content);
if(index > 0) {
final Button left = content.findViewById(R.id.image_left);
@ -121,7 +122,7 @@ public class ImagePopup extends AsyncTask<String, Void, Boolean> {
}
}
private void setImage(Bitmap btm) {
private void setImage(Bitmap btm, ImageView mImg) {
int height = (int)(btm.getHeight() / (btm.getWidth() / 640.0));
btm = Bitmap.createScaledBitmap( btm,640,height, false);
mImg.setImageBitmap(btm);

View File

@ -32,7 +32,7 @@ public class MainPage extends AsyncTask<Integer, Void, Integer> {
private WeakReference<MainActivity> ui;
private TwitterEngine mTwitter;
private RecyclerView timelineList, trendList, mentionList;
private TimelineRecycler timelineAdapter, mentionAdapter;
private TrendRecycler trendsAdapter;
private int woeid;
@ -49,17 +49,19 @@ public class MainPage extends AsyncTask<Integer, Void, Integer> {
mTwitter = TwitterEngine.getInstance(context);
SharedPreferences settings = context.getSharedPreferences("settings", 0);
woeid = settings.getInt("woeid",23424829); // Germany WOEID
timelineList = ui.get().findViewById(R.id.tl_list);
trendList = ui.get().findViewById(R.id.tr_list);
mentionList = ui.get().findViewById(R.id.m_list);
highlight = settings.getInt("highlight_color", 0xffff00ff);
font = settings.getInt("font_color", 0xffffffff);
image = settings.getBoolean("image_load", true);
RecyclerView timelineList = ui.get().findViewById(R.id.tl_list);
RecyclerView mentionList = ui.get().findViewById(R.id.m_list);
timelineAdapter = (TimelineRecycler) timelineList.getAdapter();
mentionAdapter = (TimelineRecycler) mentionList.getAdapter();
}
/**
* @param args [0] Execution Mode: (0)HomeTL, (1)Trend, (2)Mention
* @return success
* @return Mode
*/
@Override
protected Integer doInBackground(Integer... args) {
@ -72,7 +74,7 @@ public class MainPage extends AsyncTask<Integer, Void, Integer> {
TrendDatabase trendDb = new TrendDatabase(ui.get());
switch (MODE) {
case HOME:
timelineAdapter = (TimelineRecycler) timelineList.getAdapter();
if(timelineAdapter != null && timelineAdapter.getItemCount() > 0) {
id = timelineAdapter.getItemId(0);
tweets = mTwitter.getHome(page,id);
@ -98,7 +100,7 @@ public class MainPage extends AsyncTask<Integer, Void, Integer> {
case TRND:
List<Trend> trends = mTwitter.getTrends(woeid);
trendDb.store(trends);
trendsAdapter = (TrendRecycler) trendList.getAdapter();
trendsAdapter = new TrendRecycler(trends, ui.get());
trendsAdapter.setColor(font);
break;
@ -110,7 +112,6 @@ public class MainPage extends AsyncTask<Integer, Void, Integer> {
case MENT:
List<Tweet> mention;
mentionAdapter = (TimelineRecycler) mentionList.getAdapter();
if(mentionAdapter != null && mentionAdapter.getItemCount() != 0) {
id = mentionAdapter.getItemId(0);
mention = mTwitter.getMention(page,id);
@ -150,6 +151,9 @@ public class MainPage extends AsyncTask<Integer, Void, Integer> {
SwipeRefreshLayout timelineRefresh = connect.findViewById(R.id.timeline);
SwipeRefreshLayout trendRefresh = connect.findViewById(R.id.trends);
SwipeRefreshLayout mentionRefresh = connect.findViewById(R.id.mention);
RecyclerView timelineList = ui.get().findViewById(R.id.tl_list);
RecyclerView mentionList = ui.get().findViewById(R.id.m_list);
RecyclerView trendList = ui.get().findViewById(R.id.tr_list);
switch(MODE) {
case HOME:
@ -171,11 +175,11 @@ public class MainPage extends AsyncTask<Integer, Void, Integer> {
break;
case FAIL:
Toast.makeText(connect, "Fehler: "+errMsg, Toast.LENGTH_LONG).show();
default:
timelineRefresh.setRefreshing(false);
trendRefresh.setRefreshing(false);
mentionRefresh.setRefreshing(false);
Toast.makeText(connect, "Fehler: "+errMsg, Toast.LENGTH_LONG).show();
}
}
}

View File

@ -41,15 +41,14 @@ public class ProfileLoader extends AsyncTask<Long,Void,Long> {
private static final long IGNORE = 0x9;
private String screenName, username, description, location, follower, following;
private RecyclerView profileTweets, profileFavorits;
private String /* bannerLink,*/ profileImage, link, dateString;
private TimelineRecycler homeTl, homeFav;
private WeakReference<UserProfile> ui;
private TwitterEngine mTwitter;
private String errMsg = "";
private int font, highlight;
private boolean imgEnabled;
private boolean isHome = false;
private boolean imgEnabled = false;
private boolean isFollowing = false;
private boolean isFollowed = false;
private boolean isVerified = false;
@ -62,13 +61,16 @@ public class ProfileLoader extends AsyncTask<Long,Void,Long> {
*/
public ProfileLoader(Context context) {
ui = new WeakReference<>((UserProfile)context);
profileTweets = ui.get().findViewById(R.id.ht_list);
profileFavorits = ui.get().findViewById(R.id.hf_list);
mTwitter = TwitterEngine.getInstance(context);
SharedPreferences settings = context.getSharedPreferences("settings", 0);
font = settings.getInt("font_color", 0xffffffff);
highlight = settings.getInt("highlight_color", 0xffff00ff);
imgEnabled = settings.getBoolean("image_load",true);
RecyclerView profileTweets = ui.get().findViewById(R.id.ht_list);
RecyclerView profileFavorits = ui.get().findViewById(R.id.hf_list);
homeTl = (TimelineRecycler) profileTweets.getAdapter();
homeFav = (TimelineRecycler) profileFavorits.getAdapter();
}
@ -116,7 +118,7 @@ public class ProfileLoader extends AsyncTask<Long,Void,Long> {
{
DatabaseAdapter tweetDb = new DatabaseAdapter(ui.get());
List<Tweet> tweets;
homeTl = (TimelineRecycler) profileTweets.getAdapter();
if(homeTl != null && homeTl.getItemCount() > 0) {
id = homeTl.getItemId(0);
tweets = mTwitter.getUserTweets(userId,args[2],id);
@ -137,7 +139,6 @@ public class ProfileLoader extends AsyncTask<Long,Void,Long> {
{
DatabaseAdapter tweetDb = new DatabaseAdapter(ui.get());
List<Tweet> favorits;
homeFav = (TimelineRecycler) profileFavorits.getAdapter();
if(homeFav != null && homeFav.getItemCount() > 0) {
id = homeFav.getItemId(0);
favorits = mTwitter.getUserFavs(userId,args[2],id);
@ -244,12 +245,14 @@ public class ProfileLoader extends AsyncTask<Long,Void,Long> {
}
else if(mode == GET_TWEETS)
{
RecyclerView profileTweets = ui.get().findViewById(R.id.ht_list);
profileTweets.setAdapter(homeTl);
SwipeRefreshLayout tweetsReload = connect.findViewById(R.id.hometweets);
tweetsReload.setRefreshing(false);
}
else if(mode == GET_FAVS)
{
RecyclerView profileFavorits = ui.get().findViewById(R.id.hf_list);
profileFavorits.setAdapter(homeFav);
SwipeRefreshLayout favoritsReload = connect.findViewById(R.id.homefavorits);
favoritsReload.setRefreshing(false);

View File

@ -33,7 +33,7 @@ public class Registration extends AsyncTask<String, Void, Boolean> {
String pin = twitterPin[0];
try {
if( pin.trim().isEmpty() ) {
mTwitter.request();
mTwitter.request(ui.get());
} else {
mTwitter.initialize(pin);
return true;

View File

@ -51,7 +51,6 @@ public class StatusLoader extends AsyncTask<Long, Void, Long> implements View.On
private TwitterEngine mTwitter;
private TimelineRecycler tlAdp;
private RecyclerView replyList;
private String usernameStr, scrNameStr, tweetStr, dateString;
private String repliedUsername, apiName, retweeter;
private String medialinks[], profile_pb;
@ -71,7 +70,8 @@ public class StatusLoader extends AsyncTask<Long, Void, Long> implements View.On
highlight = settings.getInt("highlight_color", 0xffff00ff);
toggleImg = settings.getBoolean("image_load",true);
ui = new WeakReference<>((TweetDetail)c);
replyList = ui.get().findViewById(R.id.answer_list);
RecyclerView replyList = ui.get().findViewById(R.id.answer_list);
tlAdp = (TimelineRecycler) replyList.getAdapter();
}
@ -90,7 +90,6 @@ public class StatusLoader extends AsyncTask<Long, Void, Long> implements View.On
if(tweet == null)
return IGNORE;
tlAdp = (TimelineRecycler) replyList.getAdapter();
List<Tweet> answers = dbAdp.load(DatabaseAdapter.ANS, tweetID);
tlAdp = new TimelineRecycler(answers,ui.get());
tlAdp.setColor(highlight, font);
@ -154,7 +153,6 @@ public class StatusLoader extends AsyncTask<Long, Void, Long> implements View.On
List<Tweet> answers;
DatabaseAdapter twdb = new DatabaseAdapter(ui.get());
String replyname = tweet.user.screenname;
tlAdp = (TimelineRecycler) replyList.getAdapter();
if(tlAdp != null && tlAdp.getItemCount() > 0) {
long sinceId = tlAdp.getItemId(0);
answers = mTwitter.getAnswers(replyname, tweetID, sinceId);
@ -217,7 +215,6 @@ public class StatusLoader extends AsyncTask<Long, Void, Long> implements View.On
txtRet.setText(rtStr);
if(tlAdp != null) {
replyList.setAdapter(tlAdp);
String ansStr = Integer.toString(tlAdp.getItemCount());
TextView txtAns = connect.findViewById(R.id.no_ans_detail);
txtAns.setText(ansStr);
@ -273,7 +270,6 @@ public class StatusLoader extends AsyncTask<Long, Void, Long> implements View.On
setIcons(favoriteButton, retweetButton);
}
else if(mode == LOAD_REPLY) {
replyList.setAdapter(tlAdp);
SwipeRefreshLayout ansReload = connect.findViewById(R.id.answer_reload);
ansReload.setRefreshing(false);
String ansStr = Integer.toString(tlAdp.getItemCount());

View File

@ -18,8 +18,6 @@ public class StatusUpload extends AsyncTask<Object, Void, Boolean> implements Al
private WeakReference<TweetPopup> ui;
private TwitterEngine mTwitter;
private View load;
private EditText tweet;
private String[] path;
/**
@ -29,13 +27,14 @@ public class StatusUpload extends AsyncTask<Object, Void, Boolean> implements Al
public StatusUpload(Context context, String[] path) {
ui = new WeakReference<>((TweetPopup)context);
mTwitter = TwitterEngine.getInstance(context);
load = ui.get().findViewById(R.id.tweet_sending);
tweet = (EditText) ui.get().findViewById(R.id.tweet_input);
this.path = path;
}
@Override
protected void onPreExecute() {
EditText tweet = ui.get().findViewById(R.id.tweet_input);
View load = ui.get().findViewById(R.id.tweet_sending);
load.setVisibility(View.VISIBLE);
tweet.setFocusable(false);
}
@ -80,6 +79,7 @@ public class StatusUpload extends AsyncTask<Object, Void, Boolean> implements Al
builder.setTitle("Fehler").setMessage("Tweet wurde nicht gesendet!")
.setPositiveButton(R.string.retry, this)
.setNegativeButton(R.string.cancel, this).show();
View load = ui.get().findViewById(R.id.tweet_sending);
load.setVisibility(View.INVISIBLE);
}
}

View File

@ -38,12 +38,11 @@ import twitter4j.conf.ConfigurationBuilder;
public class TwitterEngine {
private final String TWITTER_CONSUMER_KEY = "0EKRHWYcakpCkl8Lr4OcBFMZb";
private final String TWITTER_CONSUMER_SECRET = "RQrf0uQus5v7IMuYgdlVeBuLw1ApRJhxcAMM8MyUVRh1nKSxnE";
private final String TWITTER_CONSUMER_SECRET = "insert your own key";
private static TwitterEngine mTwitter;
private static long twitterID = -1L;
private Twitter twitter;
private Context context;
private SharedPreferences settings;
private RequestToken reqToken;
private boolean login;
@ -64,7 +63,6 @@ public class TwitterEngine {
Configuration configuration = builder.build();
TwitterFactory factory = new TwitterFactory(configuration);
twitter = factory.getInstance();
this.context = context;
}
@ -72,7 +70,7 @@ public class TwitterEngine {
* get RequestToken and Open Twitter Registration Website
* @throws TwitterException if Connection is unavailable
*/
public void request() throws TwitterException {
public void request(Context context) throws TwitterException {
if(reqToken == null)
reqToken = twitter.getOAuthRequestToken();
String redirectURL = reqToken.getAuthenticationURL();
@ -147,6 +145,13 @@ public class TwitterEngine {
twitterID = settings.getLong("userID", -1L);
}
/**
* set amount of tweets to be loaded
*/
private void setLoad() {
load = settings.getInt("preload", 10);
}
/**
* @return if Twitter4J is registered
*/
@ -588,7 +593,7 @@ public class TwitterEngine {
mTwitter = new TwitterEngine(context);
mTwitter.init();
}
mTwitter.load = mTwitter.settings.getInt("preload", 10);
mTwitter.setLoad();
return mTwitter;
}
}

View File

@ -23,7 +23,6 @@ public class TwitterSearch extends AsyncTask<String, Void, Void> {
private TimelineRecycler tlRc;
private UserRecycler uAdp;
private RecyclerView tweetSearch, userSearch;
private TwitterEngine mTwitter;
private WeakReference<SearchPage> ui;
private int highlight, font_color;
@ -32,14 +31,17 @@ public class TwitterSearch extends AsyncTask<String, Void, Void> {
public TwitterSearch(Context context) {
ui = new WeakReference<>((SearchPage)context);
tweetSearch = ui.get().findViewById(R.id.tweet_result);
userSearch = ui.get().findViewById(R.id.user_result);
mTwitter = TwitterEngine.getInstance(context);
SharedPreferences settings = context.getSharedPreferences("settings", 0);
font_color = settings.getInt("font_color", 0xffffffff);
highlight = settings.getInt("highlight_color", 0xffff00ff);
imageload = settings.getBoolean("image_load",true);
RecyclerView tweetSearch = ui.get().findViewById(R.id.tweet_result);
RecyclerView userSearch = ui.get().findViewById(R.id.user_result);
tlRc = (TimelineRecycler) tweetSearch.getAdapter();
uAdp = (UserRecycler) userSearch.getAdapter();
}
@ -48,9 +50,6 @@ public class TwitterSearch extends AsyncTask<String, Void, Void> {
String strSearch = search[0];
long id = 1L;
try {
tlRc = (TimelineRecycler) tweetSearch.getAdapter();
uAdp = (UserRecycler) userSearch.getAdapter();
if(tlRc != null && tlRc.getItemCount() > 0) {
id = tlRc.getItemId(0);
List<Tweet> tweets = mTwitter.searchTweets(strSearch,id);
@ -89,6 +88,8 @@ public class TwitterSearch extends AsyncTask<String, Void, Void> {
SwipeRefreshLayout tweetReload = connect.findViewById(R.id.searchtweets);
View circleLoad = connect.findViewById(R.id.search_progress);
circleLoad.setVisibility(View.INVISIBLE);
RecyclerView tweetSearch = ui.get().findViewById(R.id.tweet_result);
RecyclerView userSearch = ui.get().findViewById(R.id.user_result);
tweetSearch.setAdapter(tlRc);
userSearch.setAdapter(uAdp);
tweetReload.setRefreshing(false);

View File

@ -27,7 +27,6 @@ public class UserLists extends AsyncTask <Long, Void, Void> {
private WeakReference<UserDetail> ui;
private TwitterEngine mTwitter;
private UserRecycler usrAdp;
private RecyclerView userList;
private String errmsg;
private boolean imageload;
@ -38,7 +37,8 @@ public class UserLists extends AsyncTask <Long, Void, Void> {
public UserLists(Context context) {
ui = new WeakReference<>((UserDetail)context);
mTwitter = TwitterEngine.getInstance(context);
userList = ui.get().findViewById(R.id.userlist);
RecyclerView userList = ui.get().findViewById(R.id.userlist);
usrAdp = (UserRecycler) userList.getAdapter();
SharedPreferences settings = context.getSharedPreferences("settings", 0);
imageload = settings.getBoolean("image_load",true);
@ -51,7 +51,7 @@ public class UserLists extends AsyncTask <Long, Void, Void> {
long mode = data[1];
long cursor = data[2];
try {
usrAdp = (UserRecycler) userList.getAdapter();
if(mode == FOLLOWING) {
List<TwitterUser> user = mTwitter.getFollowing(id,cursor);
usrAdp = new UserRecycler(user,ui.get());
@ -84,6 +84,7 @@ public class UserLists extends AsyncTask <Long, Void, Void> {
mProgress.setVisibility(View.INVISIBLE);
if(errmsg == null) {
RecyclerView userList = ui.get().findViewById(R.id.userlist);
userList.setAdapter(usrAdp);
}
else {

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/preview_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">