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

Fix #41: Update activity title from webview's loadUrl.

This commit is contained in:
vanitasvitae 2016-08-01 01:14:44 +02:00
parent f2bcadd7bd
commit efba350907
2 changed files with 49 additions and 12 deletions

View file

@ -43,6 +43,7 @@ import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout; import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.NavigationView; import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.GravityCompat; import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout; import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout;
@ -112,7 +113,9 @@ public class MainActivity extends AppCompatActivity
public static final String ACTION_OPEN_URL = "com.github.dfa.diaspora_android.MainActivity.open_url"; public static final String ACTION_OPEN_URL = "com.github.dfa.diaspora_android.MainActivity.open_url";
public static final String ACTION_CHANGE_ACCOUNT = "com.github.dfa.diaspora_android.MainActivity.change_account"; public static final String ACTION_CHANGE_ACCOUNT = "com.github.dfa.diaspora_android.MainActivity.change_account";
public static final String ACTION_CLEAR_CACHE = "com.github.dfa.diaspora_android.MainActivity.clear_cache"; public static final String ACTION_CLEAR_CACHE = "com.github.dfa.diaspora_android.MainActivity.clear_cache";
public static final String ACTION_UPDATE_TITLE_FROM_URL = "com.github.dfa.diaspora_android.MainActivity.set_title";
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";
private App app; private App app;
private String podDomain; private String podDomain;
@ -220,7 +223,6 @@ public class MainActivity extends AppCompatActivity
public void onClick(View view) { public void onClick(View view) {
if (Helpers.isOnline(MainActivity.this)) { if (Helpers.isOnline(MainActivity.this)) {
webView.loadUrl("https://" + podDomain + "/notifications"); webView.loadUrl("https://" + podDomain + "/notifications");
setTitle(R.string.title_notifications);
} else { } else {
Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show();
} }
@ -505,6 +507,7 @@ public class MainActivity extends AppCompatActivity
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
registerReceiver(brLoadUrl, new IntentFilter(URL_MESSAGE)); registerReceiver(brLoadUrl, new IntentFilter(URL_MESSAGE));
LocalBroadcastManager.getInstance(this).registerReceiver(brSetTitle, new IntentFilter(ACTION_UPDATE_TITLE_FROM_URL));
} }
@Override @Override
@ -531,9 +534,42 @@ public class MainActivity extends AppCompatActivity
} }
}; };
private final BroadcastReceiver brSetTitle = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String url = intent.getStringExtra(EXTRA_URL);
if (url != null && url.startsWith("https://"+podDomain)) {
String subUrl = url.substring(("https://"+podDomain).length());
Log.d(App.TAG, "LocalBroadcastReceiver: SubUrl: "+subUrl);
if (subUrl.startsWith("/stream")) {
setTitle(R.string.title_stream);
} else if(subUrl.startsWith("/notifications")) {
setTitle(R.string.title_notifications);
} else if (subUrl.startsWith("/conversations")) {
setTitle(R.string.title_conversations);
} else if (subUrl.startsWith("/status_messages/new")) {
setTitle(R.string.new_post);
} else if (subUrl.startsWith("/people/" + appSettings.getProfileId())) {
setTitle(R.string.title_profil);
} else if (subUrl.startsWith("/activity")) {
setTitle(R.string.title_activities);
} else if (subUrl.startsWith("/liked")) {
setTitle(R.string.title_liked);
} else if (subUrl.startsWith("/commented")) {
setTitle(R.string.title_commented);
} else if (subUrl.startsWith("/mentions")) {
setTitle(R.string.title_mentions);
} else if (subUrl.startsWith("/public")) {
setTitle(R.string.title_public);
}
}
}
};
@Override @Override
protected void onPause() { protected void onPause() {
unregisterReceiver(brLoadUrl); unregisterReceiver(brLoadUrl);
LocalBroadcastManager.getInstance(this).unregisterReceiver(brSetTitle);
super.onPause(); super.onPause();
} }
@ -570,7 +606,6 @@ public class MainActivity extends AppCompatActivity
case R.id.action_notifications: { case R.id.action_notifications: {
if (Helpers.isOnline(MainActivity.this)) { if (Helpers.isOnline(MainActivity.this)) {
webView.loadUrl("https://" + podDomain + "/notifications"); webView.loadUrl("https://" + podDomain + "/notifications");
setTitle(R.string.title_notifications);
return true; return true;
} else { } else {
snackbarNoInternet.show(); snackbarNoInternet.show();
@ -581,7 +616,6 @@ public class MainActivity extends AppCompatActivity
case R.id.action_conversations: { case R.id.action_conversations: {
if (Helpers.isOnline(MainActivity.this)) { if (Helpers.isOnline(MainActivity.this)) {
webView.loadUrl("https://" + podDomain + "/conversations"); webView.loadUrl("https://" + podDomain + "/conversations");
setTitle(R.string.title_conversations);
return true; return true;
} else { } else {
snackbarNoInternet.show(); snackbarNoInternet.show();
@ -603,7 +637,6 @@ public class MainActivity extends AppCompatActivity
case R.id.action_compose: { case R.id.action_compose: {
if (Helpers.isOnline(MainActivity.this)) { if (Helpers.isOnline(MainActivity.this)) {
webView.loadUrl("https://" + podDomain + "/status_messages/new"); webView.loadUrl("https://" + podDomain + "/status_messages/new");
setTitle(R.string.new_post);
} else { } else {
snackbarNoInternet.show(); snackbarNoInternet.show();
} }
@ -877,7 +910,6 @@ public class MainActivity extends AppCompatActivity
case R.id.nav_stream: { case R.id.nav_stream: {
if (Helpers.isOnline(MainActivity.this)) { if (Helpers.isOnline(MainActivity.this)) {
webView.loadUrl("https://" + podDomain + "/stream"); webView.loadUrl("https://" + podDomain + "/stream");
setTitle(R.string.title_stream);
} else { } else {
snackbarNoInternet.show(); snackbarNoInternet.show();
} }
@ -887,7 +919,6 @@ public class MainActivity extends AppCompatActivity
case R.id.nav_profile: { case R.id.nav_profile: {
if (Helpers.isOnline(MainActivity.this)) { if (Helpers.isOnline(MainActivity.this)) {
webView.loadUrl("https://" + podDomain + "/people/" + appSettings.getProfileId()); webView.loadUrl("https://" + podDomain + "/people/" + appSettings.getProfileId());
setTitle(R.string.title_profil);
} else { } else {
snackbarNoInternet.show(); snackbarNoInternet.show();
} }
@ -919,7 +950,6 @@ public class MainActivity extends AppCompatActivity
case R.id.nav_activities: { case R.id.nav_activities: {
if (Helpers.isOnline(MainActivity.this)) { if (Helpers.isOnline(MainActivity.this)) {
webView.loadUrl("https://" + podDomain + "/activity"); webView.loadUrl("https://" + podDomain + "/activity");
setTitle(R.string.title_activities);
} else { } else {
snackbarNoInternet.show(); snackbarNoInternet.show();
} }
@ -929,7 +959,6 @@ public class MainActivity extends AppCompatActivity
case R.id.nav_liked: { case R.id.nav_liked: {
if (Helpers.isOnline(MainActivity.this)) { if (Helpers.isOnline(MainActivity.this)) {
webView.loadUrl("https://" + podDomain + "/liked"); webView.loadUrl("https://" + podDomain + "/liked");
setTitle(R.string.title_liked);
} else { } else {
snackbarNoInternet.show(); snackbarNoInternet.show();
} }
@ -939,7 +968,6 @@ public class MainActivity extends AppCompatActivity
case R.id.nav_commented: { case R.id.nav_commented: {
if (Helpers.isOnline(MainActivity.this)) { if (Helpers.isOnline(MainActivity.this)) {
webView.loadUrl("https://" + podDomain + "/commented"); webView.loadUrl("https://" + podDomain + "/commented");
setTitle(R.string.title_commented);
} else { } else {
snackbarNoInternet.show(); snackbarNoInternet.show();
} }
@ -949,7 +977,6 @@ public class MainActivity extends AppCompatActivity
case R.id.nav_mentions: { case R.id.nav_mentions: {
if (Helpers.isOnline(MainActivity.this)) { if (Helpers.isOnline(MainActivity.this)) {
webView.loadUrl("https://" + podDomain + "/mentions"); webView.loadUrl("https://" + podDomain + "/mentions");
setTitle(R.string.title_mentions);
} else { } else {
snackbarNoInternet.show(); snackbarNoInternet.show();
} }
@ -959,7 +986,6 @@ public class MainActivity extends AppCompatActivity
case R.id.nav_public: { case R.id.nav_public: {
if (Helpers.isOnline(MainActivity.this)) { if (Helpers.isOnline(MainActivity.this)) {
webView.loadUrl("https://" + podDomain + "/public"); webView.loadUrl("https://" + podDomain + "/public");
setTitle(R.string.title_public);
} else { } else {
snackbarNoInternet.show(); snackbarNoInternet.show();
} }

View file

@ -31,11 +31,14 @@ import android.content.pm.PackageManager;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.net.Uri; import android.net.Uri;
import android.os.Environment; import android.os.Environment;
import android.support.v4.content.LocalBroadcastManager;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log;
import android.view.ContextMenu; import android.view.ContextMenu;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.Toast; import android.widget.Toast;
import com.github.dfa.diaspora_android.App;
import com.github.dfa.diaspora_android.R; import com.github.dfa.diaspora_android.R;
import com.github.dfa.diaspora_android.activity.MainActivity; import com.github.dfa.diaspora_android.activity.MainActivity;
import com.github.dfa.diaspora_android.task.ImageDownloadTask; import com.github.dfa.diaspora_android.task.ImageDownloadTask;
@ -137,7 +140,7 @@ public class ContextMenuWebView extends NestedWebView {
} else { } else {
Toast.makeText(context, "Cannot share image: url is null", Toast.LENGTH_SHORT).show(); Toast.makeText(context, "Cannot share image: url is null", Toast.LENGTH_SHORT).show();
} }
break; break;
case ID_IMAGE_EXTERNAL_BROWSER: case ID_IMAGE_EXTERNAL_BROWSER:
if (url != null) { if (url != null) {
@ -188,6 +191,14 @@ public class ContextMenuWebView extends NestedWebView {
} }
} }
@Override
public void loadUrl(String url) {
super.loadUrl(url);
Intent updateActivityTitleIntent = new Intent(MainActivity.ACTION_UPDATE_TITLE_FROM_URL);
updateActivityTitleIntent.putExtra(MainActivity.EXTRA_URL, getUrl());
LocalBroadcastManager.getInstance(context).sendBroadcast(updateActivityTitleIntent);
}
public void setParentActivity(Activity activity) { public void setParentActivity(Activity activity) {
this.parentActivity = activity; this.parentActivity = activity;
} }