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

Allow to jump to last visited page on stream; FIX NullPtr in shared text methods

This commit is contained in:
Gregor Santner 2016-12-18 15:07:00 +01:00
parent d567b5fbdd
commit 4f3eac9977
No known key found for this signature in database
GPG key ID: 2AEB822A5CF48C9F
8 changed files with 125 additions and 40 deletions

View file

@ -28,6 +28,7 @@ import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.design.widget.Snackbar;
import android.view.Menu;
@ -231,7 +232,10 @@ public class DiasporaStreamFragment extends BrowserFragment {
protected DiasporaStreamWebChromeClient.SharedTextCallback sharedTextCallback = new DiasporaStreamWebChromeClient.SharedTextCallback() {
@Override
public String getSharedText() {
return ((MainActivity) getActivity()).getTextToBeShared();
if(getActivity() != null) {
return ((MainActivity) getActivity()).getTextToBeShared();
}
return "";
}
@Override
@ -328,11 +332,16 @@ public class DiasporaStreamFragment extends BrowserFragment {
@SuppressWarnings("unused")
@JavascriptInterface
public void setUserProfile(final String webMessage) throws JSONException {
DiasporaUserProfile pup = ((App) getActivity().getApplication()).getDiasporaUserProfile();
final DiasporaUserProfile pup = ((App) getActivity().getApplication()).getDiasporaUserProfile();
AppLog.spam(this, "StreamFragment.JavaScriptInterface.setUserProfile()");
if (pup.isRefreshNeeded()) {
AppLog.v(this, "DiasporaUserProfile needs refresh; Try to parse JSON");
pup.parseJson(webMessage);
getActivity().runOnUiThread(new Runnable() {
public void run() {
pup.analyzeUrl(webView.getUrl());
}
});
} else {
AppLog.spam(this, "No DiasporaUserProfile refresh needed");
}
@ -341,7 +350,9 @@ public class DiasporaStreamFragment extends BrowserFragment {
@SuppressWarnings("unused")
@JavascriptInterface
public void contentHasBeenShared() {
((MainActivity) getActivity()).setTextToBeShared(null);
if (getActivity() != null) {
((MainActivity) getActivity()).setTextToBeShared(null);
}
}
}

View file

@ -115,8 +115,7 @@ public class MainActivity extends ThemedActivity
private final Handler uiHandler = new Handler();
private OpenExternalLinkReceiver brOpenExternalLink;
private BroadcastReceiver brSetTitle;
private Snackbar snackbarExitApp;
private Snackbar snackbarNoInternet;
private Snackbar snackbarExitApp, snackbarNoInternet, snackbarLastVisitedTimestampInStream;
private FragmentManager fm;
private CustomTabsSession customTabsSession;
@ -182,14 +181,15 @@ public class MainActivity extends ThemedActivity
brOpenExternalLink = new OpenExternalLinkReceiver(this);
brSetTitle = new UpdateTitleReceiver(app, urls, new UpdateTitleReceiver.TitleCallback() {
public void setTitle(int resId) {
public void setTitle(String url, int resId) {
CustomFragment top = getTopFragment();
if (top != null && top.getFragmentTag().equals(DiasporaStreamFragment.TAG)) {
MainActivity.this.setTitle(resId);
showLastVisitedTimestampMessageIfNeeded(url);
}
}
public void setTitle(String title) {
public void setTitle(String url, String title) {
CustomFragment top = getTopFragment();
if (top != null && top.getFragmentTag().equals(DiasporaStreamFragment.TAG)) {
MainActivity.this.setTitle(title);
@ -238,12 +238,17 @@ public class MainActivity extends ThemedActivity
snackbarExitApp = Snackbar
.make(fragmentContainer, R.string.confirm_exit, Snackbar.LENGTH_LONG)
.setAction(android.R.string.yes, new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
moveTaskToBack(true);
}
});
snackbarLastVisitedTimestampInStream = Snackbar.make(fragmentContainer, R.string.jump_to_last_visited_timestamp_in_stream, Snackbar.LENGTH_SHORT)
.setAction(android.R.string.yes, new View.OnClickListener() {
public void onClick(View view) {
openDiasporaUrl(urls.getStreamWithTimestampUrl(diasporaUserProfile.getLastVisitedPositionInStream()));
}
});
snackbarNoInternet = Snackbar.make(fragmentContainer, R.string.no_internet, Snackbar.LENGTH_LONG);
// Load app settings
@ -251,18 +256,6 @@ public class MainActivity extends ThemedActivity
AppLog.v(this, "UI successfully set up");
}
/**
* Show DiasporaStreamFragment if necessary and load URL url
*
* @param url URL to load in the DiasporaStreamFragment
*/
public void openDiasporaUrl(String url) {
AppLog.v(this, "openDiasporaUrl()");
DiasporaStreamFragment streamFragment = (DiasporaStreamFragment) getFragment(DiasporaStreamFragment.TAG);
showFragment(streamFragment);
streamFragment.loadUrl(url);
}
/**
* Get an instance of the CustomFragment with the tag fragmentTag.
* If there was no instance so far, create a new one and add it to the FragmentManagers pool.
@ -305,6 +298,26 @@ public class MainActivity extends ThemedActivity
}
}
/**
* Show DiasporaStreamFragment if necessary and load URL url
*
* @param url URL to load in the DiasporaStreamFragment
*/
public void openDiasporaUrl(String url) {
AppLog.v(this, "openDiasporaUrl()");
DiasporaStreamFragment streamFragment = (DiasporaStreamFragment) getFragment(DiasporaStreamFragment.TAG);
showFragment(streamFragment);
showLastVisitedTimestampMessageIfNeeded(url);
streamFragment.loadUrl(url);
}
public void showLastVisitedTimestampMessageIfNeeded(String url){
if (url.equals(urls.getStreamUrl()) && diasporaUserProfile.hasLastVisitedTimestampInStream()){
snackbarLastVisitedTimestampInStream.show();
diasporaUserProfile.resetLastVisitedPositionInStream();
}
}
/**
* Show the Fragment fragment in R.id.fragment_container. If the fragment was already visible, do nothing.
*

View file

@ -18,12 +18,14 @@
*/
package com.github.dfa.diaspora_android.data;
import android.net.ParseException;
import android.os.Handler;
import com.github.dfa.diaspora_android.App;
import com.github.dfa.diaspora_android.listener.DiasporaUserProfileChangedListener;
import com.github.dfa.diaspora_android.util.AppLog;
import com.github.dfa.diaspora_android.util.AppSettings;
import com.github.dfa.diaspora_android.util.DiasporaUrlHelper;
import org.json.JSONArray;
import org.json.JSONException;
@ -40,6 +42,7 @@ public class DiasporaUserProfile {
private DiasporaUserProfileChangedListener listener;
private final App app;
private final AppSettings appSettings;
DiasporaUrlHelper urls;
private JSONObject json;
private long userProfileLastLoadedTimestamp;
private boolean isWebUserProfileLoaded;
@ -51,15 +54,17 @@ public class DiasporaUserProfile {
private String[] followedTags;
private int notificationCount;
private int unreadMessagesCount;
private long lastVisitedPositionInStream = -1;
public DiasporaUserProfile(App app) {
this.app = app;
appSettings = app.getSettings();
urls = new DiasporaUrlHelper(appSettings);
loadFromAppSettings();
}
public void loadFromAppSettings(){
public void loadFromAppSettings() {
avatarUrl = appSettings.getAvatarUrl();
guid = appSettings.getProfileId();
name = appSettings.getName();
@ -67,6 +72,7 @@ public class DiasporaUserProfile {
followedTags = appSettings.getFollowedTags();
notificationCount = appSettings.getNotificationCount();
unreadMessagesCount = appSettings.getUnreadMessageCount();
lastVisitedPositionInStream = appSettings.getLastVisitedPositionInStream();
}
public DiasporaUserProfile(App app, Handler callbackHandler, DiasporaUserProfileChangedListener listener) {
@ -137,6 +143,16 @@ public class DiasporaUserProfile {
return isWebUserProfileLoaded;
}
public void analyzeUrl(String url) {
String prefix = urls.getPodUrl() + DiasporaUrlHelper.SUBURL_STREAM_WITH_TIMESTAMP;
if (url.startsWith(prefix)) {
try {
setLastVisitedPositionInStream(Long.parseLong(url.replace(prefix, "")));
} catch (NumberFormatException ignored) {
}
}
}
/*
// Getters & Setters
*/
@ -169,6 +185,23 @@ public class DiasporaUserProfile {
return followedTags;
}
public long getLastVisitedPositionInStream() {
return lastVisitedPositionInStream;
}
public void setLastVisitedPositionInStream(long lastVisitedPositionInStream) {
this.lastVisitedPositionInStream = lastVisitedPositionInStream;
appSettings.setLastVisitedPositionInStream(lastVisitedPositionInStream);
}
public boolean hasLastVisitedTimestampInStream() {
return appSettings.getLastVisitedPositionInStream() != -1;
}
public void resetLastVisitedPositionInStream() {
appSettings.setLastVisitedPositionInStream(-1);
}
/*
* Private property setters
*/

View file

@ -38,6 +38,7 @@ public class UpdateTitleReceiver extends BroadcastReceiver {
private AppSettings appSettings;
private App app;
private TitleCallback callback;
private String lastUrl;
public UpdateTitleReceiver(App app, DiasporaUrlHelper urls, TitleCallback callback) {
this.urls = urls;
@ -48,9 +49,9 @@ public class UpdateTitleReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String url = intent.getStringExtra(MainActivity.EXTRA_URL);
if (url != null && url.startsWith(urls.getPodUrl())) {
String subUrl = url.substring((urls.getPodUrl()).length());
lastUrl = intent.getStringExtra(MainActivity.EXTRA_URL);
if (lastUrl != null && lastUrl.startsWith(urls.getPodUrl())) {
String subUrl = lastUrl.substring((urls.getPodUrl()).length());
AppLog.spam(this, "onReceive()- Set title for subUrl " + subUrl);
if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_STREAM)) {
setTitle(R.string.nav_stream);
@ -74,24 +75,24 @@ public class UpdateTitleReceiver extends BroadcastReceiver {
setTitle(R.string.nav_mentions);
} else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_PUBLIC)) {
setTitle(R.string.public_);
} else if (urls.isAspectUrl(url)) {
setTitle(urls.getAspectNameFromUrl(url, app));
} else if (urls.isAspectUrl(lastUrl)) {
setTitle(urls.getAspectNameFromUrl(lastUrl, app));
}
} else {
AppLog.spam(this, "onReceive()- Invalid url: " + url);
AppLog.spam(this, "onReceive()- Invalid url: " + lastUrl);
}
}
private void setTitle(int rId) {
callback.setTitle(rId);
callback.setTitle(lastUrl, rId);
}
private void setTitle(String title) {
callback.setTitle(title);
callback.setTitle(lastUrl, title);
}
public interface TitleCallback {
void setTitle(int resId);
void setTitle(String title);
void setTitle(String url, int resId);
void setTitle(String url, String title);
}
}

View file

@ -87,6 +87,10 @@ public class AppSettings {
pref.edit().putInt(context.getString(keyRessourceId), value).apply();
}
private void setLong(SharedPreferences pref, int keyRessourceId, long value) {
pref.edit().putLong(context.getString(keyRessourceId), value).apply();
}
private void setBool(SharedPreferences pref, int keyRessourceId, boolean value) {
pref.edit().putBoolean(context.getString(keyRessourceId), value).apply();
}
@ -123,11 +127,23 @@ public class AppSettings {
private int getInt(SharedPreferences pref, int ressourceId, int defaultValue) {
return pref.getInt(context.getString(ressourceId), defaultValue);
}
private long getLong(SharedPreferences pref, int ressourceId, long defaultValue) {
return pref.getLong(context.getString(ressourceId), defaultValue);
}
public int getColor(SharedPreferences pref, String key, int defaultColor) {
return pref.getInt(key, defaultColor);
}
public void registerPrefAppPreferenceChangedListener(SharedPreferences.OnSharedPreferenceChangeListener listener) {
prefApp.registerOnSharedPreferenceChangeListener(listener);
}
public void unregisterPrefAppPreferenceChangedListener(SharedPreferences.OnSharedPreferenceChangeListener listener) {
prefApp.unregisterOnSharedPreferenceChangeListener(listener);
}
/*
// Setters & Getters
@ -396,6 +412,14 @@ public class AppSettings {
return getString(prefApp, R.string.pref_key__screen_rotation, R.string.rotation_val_system);
}
public long getLastVisitedPositionInStream(){
return getLong(prefPod, R.string.pref_key__podprofile_last_stream_position, -1);
}
public void setLastVisitedPositionInStream(long timestamp){
setLong(prefPod, R.string.pref_key__podprofile_last_stream_position, timestamp);
}
public void setPrimaryColorSettings(int base, int shade) {
setInt(prefApp, R.string.pref_key__primary_color_base, base);
setInt(prefApp, R.string.pref_key__primary_color_shade, shade);
@ -431,12 +455,4 @@ public class AppSettings {
public boolean isExtendedNotificationsActivated() {
return getBoolean(prefApp, R.string.pref_key__extended_notifications, false);
}
public void registerPrefAppPreferenceChangedListener(SharedPreferences.OnSharedPreferenceChangeListener listener) {
prefApp.registerOnSharedPreferenceChangeListener(listener);
}
public void unregisterPrefAppPreferenceChangedListener(SharedPreferences.OnSharedPreferenceChangeListener listener) {
prefApp.unregisterOnSharedPreferenceChangeListener(listener);
}
}

View file

@ -34,6 +34,7 @@ public class DiasporaUrlHelper {
public static final String SUBURL_NOTIFICATIONS = "/notifications";
public static final String SUBURL_POSTS = "/posts/";
public static final String SUBURL_STREAM = "/stream";
public static final String SUBURL_STREAM_WITH_TIMESTAMP = SUBURL_STREAM+"?max_time=";
public static final String SUBURL_CONVERSATIONS = "/conversations";
public static final String SUBURL_NEW_POST = "/status_messages/new";
public static final String SUBURL_PEOPLE = "/people/";
@ -90,6 +91,15 @@ public class DiasporaUrlHelper {
return getPodUrl() + SUBURL_STREAM;
}
/**
* Return a url that points to the stream of the configured diaspora account on a timestamp
*
* @return https://(pod-domain.tld)/stream?max_time=1482057867
*/
public String getStreamWithTimestampUrl(long timestamp) {
return getPodUrl() + SUBURL_STREAM_WITH_TIMESTAMP + timestamp;
}
/**
* Return a url that points to the notifications feed of the configured diaspora account
*

View file

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Key Names (Untranslatable) -->
<string name="pref_key__poddomain_legacy" translatable="false">podDomain</string>
<string name="pref_key__current_pod_0" translatable="false">pref_key__current_pod_0</string>
<string name="pref_key__font_size" translatable="false">pref_key_font_size</string>
<string name="pref_key__intellihide_toolbars" translatable="false">pref_key_intellihide_toolbars</string>
@ -59,6 +58,7 @@
<string name="pref_key__podprofile_followed_tags_favs" translatable="false">podUserProfile_followedTags_favs</string>
<string name="pref_key__podprofile_unread_message_count" translatable="false">podUserProfile_unreadMessageCount</string>
<string name="pref_key__podprofile_notification_count" translatable="false">podUserProfile_NotificationCount</string>
<string name="pref_key__podprofile_last_stream_position" translatable="false">podUserProfile_LastStreamPosition</string>
<!-- More -->
<string name="pref_key__logging_spam_enabled" translatable="false">pref_key__logging_spam_enabled</string>

View file

@ -113,4 +113,5 @@
<string name="https" translatable="false">https</string>
<string name="pod_address">Pod address</string>
<string name="missing_value">Missing value</string>
<string name="jump_to_last_visited_timestamp_in_stream">Jump to last visited page?</string>
</resources>