1
0
Fork 0
mirror of https://github.com/gsantner/dandelion synced 2024-11-22 04:12:08 +01:00

Made hashtags in textviews clickable. Hashtags open post editor with hashtag inserted. Also cleaned up text sharing in general

This commit is contained in:
vanitasvitae 2016-08-27 00:04:07 +02:00
parent f26f1539ec
commit 240551f34c
7 changed files with 129 additions and 38 deletions

View file

@ -12,6 +12,9 @@
android:name="com.github.dfa.diaspora_android.App" android:name="com.github.dfa.diaspora_android.App"
android:label="@string/app_name" android:label="@string/app_name"
android:theme="@style/AppTheme" > android:theme="@style/AppTheme" >
<provider
android:name="com.github.dfa.diaspora_android.data.HashtagProvider"
android:authorities="com.github.dfa.diaspora_android.mainactivity" />
<activity <activity
android:name="com.github.dfa.diaspora_android.activity.SplashActivity" android:name="com.github.dfa.diaspora_android.activity.SplashActivity"
android:launchMode="singleInstance" android:launchMode="singleInstance"
@ -60,6 +63,13 @@
<data android:mimeType="text/plain" /> <data android:mimeType="text/plain" />
</intent-filter> </intent-filter>
<!-- Hashtag click intent from HtmlTextViews -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.item/vnd.cc.tag" />
</intent-filter>
<!-- Intent from Web Browser / Mail / IM / Clickable Link --> <!-- Intent from Web Browser / Mail / IM / Clickable Link -->
<intent-filter> <intent-filter>
<action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.VIEW" />

View file

@ -1,5 +1,9 @@
package com.github.dfa.diaspora_android.activity; package com.github.dfa.diaspora_android.activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Bundle; import android.os.Bundle;
@ -29,6 +33,12 @@ public class AboutActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about); setContentView(R.layout.activity_about);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("INTREC", intent.toString());
}
}, new IntentFilter(Intent.ACTION_VIEW));
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_white_24px)); toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_white_24px));

View file

@ -114,6 +114,7 @@ public class MainActivity extends AppCompatActivity
public static final String ACTION_RELOAD_ACTIVITY = "com.github.dfa.diaspora_android.MainActivity.reload_activity"; public static final String ACTION_RELOAD_ACTIVITY = "com.github.dfa.diaspora_android.MainActivity.reload_activity";
public static final String URL_MESSAGE = "URL_MESSAGE"; public static final String URL_MESSAGE = "URL_MESSAGE";
public static final String EXTRA_URL = "com.github.dfa.diaspora_android.extra_url"; public static final String EXTRA_URL = "com.github.dfa.diaspora_android.extra_url";
public static final String CONTENT_HASHTAG = "content://com.github.dfa.diaspora_android.mainactivity/";
private App app; private App app;
private ValueCallback<Uri[]> mFilePathCallback; private ValueCallback<Uri[]> mFilePathCallback;
@ -457,11 +458,16 @@ public class MainActivity extends AppCompatActivity
String type = intent.getType(); String type = intent.getType();
String loadUrl = null; String loadUrl = null;
if (ACTION_OPEN_URL.equals(action)) { if (ACTION_OPEN_URL.equals(action)) {
loadUrl = intent.getStringExtra(URL_MESSAGE); loadUrl = intent.getStringExtra(URL_MESSAGE);
} else if (Intent.ACTION_VIEW.equals(action) && intent.getDataString() != null) { } else if (Intent.ACTION_VIEW.equals(action) && intent.getDataString() != null) {
loadUrl = intent.getDataString(); Uri data = intent.getData();
if(data != null && data.toString().startsWith(CONTENT_HASHTAG)) {
handleHashtag(intent);
return;
} else {
loadUrl = intent.getDataString();
}
} else if (ACTION_CHANGE_ACCOUNT.equals(action)) { } else if (ACTION_CHANGE_ACCOUNT.equals(action)) {
app.resetPodData(webView); app.resetPodData(webView);
Helpers.animateToActivity(MainActivity.this, PodSelectionActivity.class, true); Helpers.animateToActivity(MainActivity.this, PodSelectionActivity.class, true);
@ -858,18 +864,13 @@ public class MainActivity extends AppCompatActivity
app.getAvatarImageLoader().startImageDownload(navheaderImage, avatarUrl); app.getAvatarImageLoader().startImageDownload(navheaderImage, avatarUrl);
} }
private void handleHashtag(Intent intent) {
setSharedTexts(null, intent.getData().toString().split("/")[3]);
webView.loadUrlNew(urls.getNewPostUrl());
}
private void handleSendText(Intent intent) { private void handleSendText(Intent intent) {
String content = WebHelper.replaceUrlWithMarkdown(intent.getStringExtra(Intent.EXTRA_TEXT)); setSharedTexts(null, intent.getStringExtra(Intent.EXTRA_TEXT));
if (appSettings.isAppendSharedViaApp()) {
// &#10; = \n
content = content + "\n\n" + getString(R.string.shared_by_diaspora_android);
}
final String sharedText = WebHelper.escapeHtmlText(content);
if (sharedText != null) {
textToBeShared = sharedText;
}
webView.loadUrlNew(urls.getBlankUrl()); webView.loadUrlNew(urls.getBlankUrl());
webView.loadUrlNew(urls.getNewPostUrl()); webView.loadUrlNew(urls.getNewPostUrl());
} }
@ -880,23 +881,35 @@ public class MainActivity extends AppCompatActivity
* @param intent intent * @param intent intent
*/ */
private void handleSendSubject(Intent intent) { private void handleSendSubject(Intent intent) {
webView.loadUrlNew(urls.getNewPostUrl()); setSharedTexts(intent.getStringExtra(Intent.EXTRA_SUBJECT), intent.getStringExtra(Intent.EXTRA_TEXT));
String content = WebHelper.replaceUrlWithMarkdown(intent.getStringExtra(Intent.EXTRA_TEXT));
String subject = WebHelper.replaceUrlWithMarkdown(intent.getStringExtra(Intent.EXTRA_SUBJECT));
if (appSettings.isAppendSharedViaApp()) {
// &#10; = \n
content = content + "\n\n" + getString(R.string.shared_by_diaspora_android);
}
final String sharedSubject = WebHelper.escapeHtmlText(subject);
final String sharedContent = WebHelper.escapeHtmlText(content);
textToBeShared = "**" + sharedSubject + "** " + sharedContent;
webView.loadUrlNew(urls.getBlankUrl()); webView.loadUrlNew(urls.getBlankUrl());
webView.loadUrlNew(urls.getNewPostUrl()); webView.loadUrlNew(urls.getNewPostUrl());
} }
/**
* Set sharedText variable to escaped and formatted subject + body.
* If subject is null, only the body will be set. Else the subject will be set as header.
* Depending on whether the user has the setting isAppendSharedViaApp set, a reference to
* the app will be added at the bottom
* @param sharedSubject post subject or null
* @param sharedBody post text
*/
private void setSharedTexts(String sharedSubject, String sharedBody) {
String body = WebHelper.replaceUrlWithMarkdown(sharedBody);
if (appSettings.isAppendSharedViaApp()) {
body = body + "\n\n" + getString(R.string.shared_by_diaspora_android);
}
final String escapedBody = WebHelper.escapeHtmlText(body);
if(sharedSubject != null) {
String escapedSubject = WebHelper.escapeHtmlText(WebHelper.replaceUrlWithMarkdown(sharedSubject));
textToBeShared = "**" + escapedSubject + "** " + escapedBody;
} else {
textToBeShared = escapedBody;
}
}
//TODO: Implement? //TODO: Implement?
private void handleSendImage(Intent intent) { private void handleSendImage(Intent intent) {
final Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM); final Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);

View file

@ -0,0 +1,46 @@
package com.github.dfa.diaspora_android.data;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
public class HashtagProvider extends ContentProvider {
@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri arg0) {
return "vnd.android.cursor.item/vnd.cc.tag";
}
@Override
public Uri insert(Uri arg0, ContentValues arg1) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
return false;
}
@Override
public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
String arg4) {
// TODO Auto-generated method stub
return null;
}
@Override
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
// TODO Auto-generated method stub
return 0;
}
}

View file

@ -6,9 +6,13 @@ import android.text.Html;
import android.text.SpannableString; import android.text.SpannableString;
import android.text.util.Linkify; import android.text.util.Linkify;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Patterns;
import android.widget.TextView; import android.widget.TextView;
import com.github.dfa.diaspora_android.R; import com.github.dfa.diaspora_android.activity.MainActivity;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HtmlTextView extends TextView { public class HtmlTextView extends TextView {
@ -34,8 +38,19 @@ public class HtmlTextView extends TextView {
} }
private void init(){ private void init(){
final SpannableString content = new SpannableString(Html.fromHtml(getText().toString())); setText(new SpannableString(Html.fromHtml(getText().toString())));
Linkify.addLinks(content, Linkify.WEB_URLS); Linkify.TransformFilter filter = new Linkify.TransformFilter() {
setText(content); public final String transformUrl(final Matcher match, String url) {
return match.group();
}
};
Pattern hashtagPattern = Pattern.compile("[#]+[A-Za-z0-9-_]+\\b");
String hashtagScheme = MainActivity.CONTENT_HASHTAG;
Linkify.addLinks(this, hashtagPattern, hashtagScheme, null, filter);
Pattern urlPattern = Patterns.WEB_URL;
Linkify.addLinks(this, urlPattern, null, null, filter);
} }
} }

View file

@ -39,7 +39,6 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/fragment_about__about_content" android:text="@string/fragment_about__about_content"
style="@android:style/TextAppearance.DeviceDefault.Small" style="@android:style/TextAppearance.DeviceDefault.Small"
android:linksClickable="true" android:linksClickable="true" />
android:autoLink="web"/>
</LinearLayout> </LinearLayout>
</android.support.v4.widget.NestedScrollView> </android.support.v4.widget.NestedScrollView>

View file

@ -26,9 +26,8 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/fragment_license__license_content" android:text="@string/fragment_license__license_content"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Small" style="@android:style/TextAppearance.DeviceDefault.Small"
android:linksClickable="true" android:linksClickable="true" />
android:autoLink="web"/>
<TextView <TextView
android:layout_width="match_parent" android:layout_width="match_parent"
@ -41,8 +40,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/fragment_license__3rd_party_libs_content" android:text="@string/fragment_license__3rd_party_libs_content"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Small" style="@android:style/TextAppearance.DeviceDefault.Small"
android:linksClickable="true" android:linksClickable="true" />
android:autoLink="web"/>
</LinearLayout> </LinearLayout>
</android.support.v4.widget.NestedScrollView> </android.support.v4.widget.NestedScrollView>