moved link opener to TweetDetail activity

This commit is contained in:
NudeDude 2019-04-02 17:32:27 +02:00
parent eb4c3b8cb3
commit 4049af2fc5
4 changed files with 126 additions and 224 deletions

View File

@ -23,16 +23,6 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="*.twitter.com"
android:pathPattern="\\/.*\\/status\\/.*"
android:scheme="https" />
</intent-filter>
</activity>
<activity
@ -63,7 +53,19 @@
<activity
android:name=".window.TweetDetail"
android:screenOrientation="portrait"
android:theme="@style/AppTheme" />
android:theme="@style/AppTheme" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="*.twitter.com"
android:pathPattern="\\/.*\\/status\\/.*"
android:scheme="https" />
</intent-filter>
</activity>
<activity
android:name=".window.UserDetail"

View File

@ -1,7 +1,6 @@
package org.nuclearfog.twidda;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
@ -23,7 +22,6 @@ import android.widget.TabHost.TabSpec;
import org.nuclearfog.twidda.adapter.OnItemClickListener;
import org.nuclearfog.twidda.adapter.TimelineAdapter;
import org.nuclearfog.twidda.adapter.TrendAdapter;
import org.nuclearfog.twidda.backend.LinkBrowser;
import org.nuclearfog.twidda.backend.MainPage;
import org.nuclearfog.twidda.backend.items.Tweet;
import org.nuclearfog.twidda.database.GlobalSettings;
@ -61,7 +59,6 @@ public class MainActivity extends AppCompatActivity implements OnRefreshListener
private TrendAdapter trendsAdapter;
private GlobalSettings settings;
private MainPage home;
private LinkBrowser mBrowser;
private TabHost tabhost;
private int tabIndex = 0;
@ -152,12 +149,6 @@ public class MainActivity extends AppCompatActivity implements OnRefreshListener
home.execute(1);
setIndicator();
Uri link = getIntent().getData();
if (link != null) {
mBrowser = new LinkBrowser(this);
mBrowser.execute(link);
}
}
}
@ -167,9 +158,6 @@ public class MainActivity extends AppCompatActivity implements OnRefreshListener
if (home != null && home.getStatus() == RUNNING) {
home.cancel(true);
}
if (mBrowser != null && mBrowser.getStatus() == RUNNING) {
mBrowser.cancel(true);
}
super.onStop();
}

View File

@ -1,132 +0,0 @@
package org.nuclearfog.twidda.backend;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Toast;
import org.nuclearfog.twidda.MainActivity;
import org.nuclearfog.twidda.R;
import org.nuclearfog.twidda.backend.items.Tweet;
import org.nuclearfog.twidda.database.DatabaseAdapter;
import org.nuclearfog.twidda.window.TweetDetail;
import java.lang.ref.WeakReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import twitter4j.TwitterException;
public class LinkBrowser extends AsyncTask<Uri, Void, Void> {
private WeakReference<MainActivity> ui;
private TwitterEngine mTwitter;
private DatabaseAdapter mData;
private Tweet tweet;
private LayoutInflater inflater;
private TwitterException err;
private Dialog popup;
public LinkBrowser(@NonNull MainActivity context) {
ui = new WeakReference<>(context);
popup = new Dialog(context);
mData = new DatabaseAdapter(context);
mTwitter = TwitterEngine.getInstance(context);
inflater = LayoutInflater.from(context);
}
@Override
protected void onPreExecute() {
popup.requestWindowFeature(Window.FEATURE_NO_TITLE);
popup.setCanceledOnTouchOutside(false);
if (popup.getWindow() != null)
popup.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
View load = inflater.inflate(R.layout.item_load, null, false);
View cancelButton = load.findViewById(R.id.kill_button);
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popup.dismiss();
}
});
popup.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (getStatus() == Status.RUNNING) {
Toast.makeText(ui.get(), R.string.abort, Toast.LENGTH_SHORT).show();
cancel(true);
}
}
});
popup.setContentView(load);
popup.show();
}
@Override
protected Void doInBackground(Uri... links) {
try {
String path = links[0].getPath();
if (path != null) {
Pattern linkPattern = Pattern.compile("\\/@?[\\w_]+\\/status\\/\\d{1,20}");
Matcher linkMatch = linkPattern.matcher(path);
if (linkMatch.matches()) {
Pattern idPattern = Pattern.compile("\\d{1,20}");
Matcher idMatcher = idPattern.matcher(path);
if (idMatcher.find()) {
int start = idMatcher.start();
int end = idMatcher.end();
String idString = path.substring(start, end);
long tweetId = Long.parseLong(idString);
tweet = mData.getStatus(tweetId);
if (tweet == null)
tweet = mTwitter.getStatus(tweetId);
}
}
}
} catch (TwitterException err) {
this.err = err;
} catch (Exception err) {
if(err.getMessage() != null)
Log.e("LinkBrowser", err.getMessage());
else
Log.e("LinkBrowser", err.toString());
}
return null;
}
@Override
protected void onPostExecute(Void mode) {
if (ui.get() == null) return;
popup.dismiss();
if (tweet != null) {
Intent tweetActivity = new Intent(ui.get(), TweetDetail.class);
tweetActivity.putExtra("username", tweet.getUser().getScreenname());
tweetActivity.putExtra("userID", tweet.getUser().getId());
tweetActivity.putExtra("tweetID", tweet.getId());
ui.get().startActivity(tweetActivity);
} else {
if (err != null)
ErrorHandler.printError(ui.get(), err);
}
}
@Override
protected void onCancelled() {
popup.dismiss();
}
}

View File

@ -24,6 +24,7 @@ import android.widget.TextView;
import android.widget.Toast;
import org.nuclearfog.tag.Tagger.OnTagClickListener;
import org.nuclearfog.twidda.MainActivity;
import org.nuclearfog.twidda.R;
import org.nuclearfog.twidda.adapter.OnItemClickListener;
import org.nuclearfog.twidda.adapter.TimelineAdapter;
@ -31,6 +32,9 @@ import org.nuclearfog.twidda.backend.StatusLoader;
import org.nuclearfog.twidda.backend.items.Tweet;
import org.nuclearfog.twidda.database.GlobalSettings;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static android.os.AsyncTask.Status.RUNNING;
import static org.nuclearfog.twidda.backend.StatusLoader.Mode.ANS;
import static org.nuclearfog.twidda.backend.StatusLoader.Mode.DELETE;
@ -66,10 +70,18 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
setContentView(R.layout.page_tweet);
Bundle param = getIntent().getExtras();
if (param != null) {
Uri link = getIntent().getData();
if(link != null) {
getTweet(link.getPath());
}
else if (param != null) {
tweetID = param.getLong("tweetID");
username = param.getString("username");
}
else{
finish();
}
Toolbar tool = findViewById(R.id.tweet_toolbar);
setSupportActionBar(tool);
@ -150,41 +162,43 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.delete_tweet:
Builder deleteDialog = new Builder(this);
deleteDialog.setMessage(R.string.delete_tweet);
deleteDialog.setPositiveButton(R.string.yes_confirm, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (mStat != null && mStat.getStatus() == RUNNING)
mStat.cancel(true);
mStat = new StatusLoader(TweetDetail.this, DELETE);
mStat.execute(tweetID);
if(mStat != null && mStat.getStatus() != RUNNING) {
switch (item.getItemId()) {
case R.id.delete_tweet:
Builder deleteDialog = new Builder(this);
deleteDialog.setMessage(R.string.delete_tweet);
deleteDialog.setPositiveButton(R.string.yes_confirm, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (mStat != null && mStat.getStatus() == RUNNING)
mStat.cancel(true);
mStat = new StatusLoader(TweetDetail.this, DELETE);
mStat.execute(tweetID);
}
});
deleteDialog.setNegativeButton(R.string.no_confirm, null);
deleteDialog.show();
break;
case R.id.tweet_link:
if (mConnect.getActiveNetworkInfo() != null && mConnect.getActiveNetworkInfo().isConnected()) {
Intent intent = new Intent(Intent.ACTION_VIEW);
String tweetLink = "https://twitter.com/" + username.substring(1) + "/status/" + tweetID;
intent.setData(Uri.parse(tweetLink));
startActivity(intent);
} else {
Toast.makeText(this, R.string.connection_failed, Toast.LENGTH_SHORT).show();
}
});
deleteDialog.setNegativeButton(R.string.no_confirm, null);
deleteDialog.show();
break;
break;
case R.id.tweet_link:
if (mConnect.getActiveNetworkInfo() != null && mConnect.getActiveNetworkInfo().isConnected()) {
Intent intent = new Intent(Intent.ACTION_VIEW);
case R.id.link_copy:
String tweetLink = "https://twitter.com/" + username.substring(1) + "/status/" + tweetID;
intent.setData(Uri.parse(tweetLink));
startActivity(intent);
} else {
Toast.makeText(this, R.string.connection_failed, Toast.LENGTH_SHORT).show();
}
break;
case R.id.link_copy:
String tweetLink = "https://twitter.com/" + username.substring(1) + "/status/" + tweetID;
ClipboardManager clip = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData linkClip = ClipData.newPlainText("tweet link", tweetLink);
clip.setPrimaryClip(linkClip);
Toast.makeText(this, R.string.copied_to_clipboard, Toast.LENGTH_SHORT).show();
break;
ClipboardManager clip = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData linkClip = ClipData.newPlainText("tweet link", tweetLink);
clip.setPrimaryClip(linkClip);
Toast.makeText(this, R.string.copied_to_clipboard, Toast.LENGTH_SHORT).show();
break;
}
}
return super.onOptionsItemSelected(item);
}
@ -192,46 +206,45 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
@Override
public void onClick(View v) {
if (mStat != null && mStat.getStatus() == RUNNING)
mStat.cancel(true);
if (mStat != null && mStat.getStatus() != RUNNING) {
switch (v.getId()) {
case R.id.rt_button_detail:
if (mStat != null && mStat.getStatus() == RUNNING)
mStat.cancel(true);
mStat = new StatusLoader(this, RETWEET);
mStat.execute(tweetID);
Toast.makeText(this, R.string.loading, Toast.LENGTH_SHORT).show();
break;
switch (v.getId()) {
case R.id.rt_button_detail:
if (mStat != null && mStat.getStatus() == RUNNING)
mStat.cancel(true);
mStat = new StatusLoader(this, RETWEET);
mStat.execute(tweetID);
Toast.makeText(this, R.string.loading, Toast.LENGTH_SHORT).show();
break;
case R.id.fav_button_detail:
if (mStat != null && mStat.getStatus() == RUNNING)
mStat.cancel(true);
mStat = new StatusLoader(this, FAVORITE);
mStat.execute(tweetID);
Toast.makeText(this, R.string.loading, Toast.LENGTH_SHORT).show();
break;
case R.id.fav_button_detail:
if (mStat != null && mStat.getStatus() == RUNNING)
mStat.cancel(true);
mStat = new StatusLoader(this, FAVORITE);
mStat.execute(tweetID);
Toast.makeText(this, R.string.loading, Toast.LENGTH_SHORT).show();
break;
case R.id.no_rt_detail:
Intent retweet = new Intent(this, UserDetail.class);
retweet.putExtra("tweetID", tweetID);
retweet.putExtra("mode", 2);
startActivity(retweet);
break;
case R.id.no_rt_detail:
Intent retweet = new Intent(this, UserDetail.class);
retweet.putExtra("tweetID", tweetID);
retweet.putExtra("mode", 2);
startActivity(retweet);
break;
case R.id.no_fav_detail:
Intent favorit = new Intent(this, UserDetail.class);
favorit.putExtra("tweetID", tweetID);
favorit.putExtra("mode", 3);
startActivity(favorit);
break;
case R.id.no_fav_detail:
Intent favorit = new Intent(this, UserDetail.class);
favorit.putExtra("tweetID", tweetID);
favorit.putExtra("mode", 3);
startActivity(favorit);
break;
case R.id.answer_button:
Intent tweet = new Intent(this, TweetPopup.class);
tweet.putExtra("TweetID", tweetID);
tweet.putExtra("Addition", username);
startActivityForResult(tweet, TWEET);
break;
case R.id.answer_button:
Intent tweet = new Intent(this, TweetPopup.class);
tweet.putExtra("TweetID", tweetID);
tweet.putExtra("Addition", username);
startActivityForResult(tweet, TWEET);
break;
}
}
}
@ -275,4 +288,35 @@ public class TweetDetail extends AppCompatActivity implements OnClickListener,
isHome = true;
invalidateOptionsMenu();
}
private void getTweet(String link) {
if (link != null) {
Pattern linkPattern = Pattern.compile("/@?[\\w_]+/status/\\d{1,20}");
Matcher linkMatch = linkPattern.matcher(link);
if (linkMatch.matches()) {
Pattern idPattern = Pattern.compile("\\d{1,20}");
Pattern usrPattern = Pattern.compile("/@?[\\w_]+/");
Matcher matcher = idPattern.matcher(link);
if (matcher.find()) {
int start = matcher.start();
int end = matcher.end();
tweetID = Long.parseLong(link.substring(start, end));
}
matcher = usrPattern.matcher(link);
if (matcher.find()) {
int start = matcher.start();
int end = matcher.end();
username = link.substring(start + 1, end - 1);
}
}
else {
Toast.makeText(this,R.string.tweet_not_found,Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
}
}