1
0
Fork 0
mirror of https://github.com/gsantner/dandelion synced 2024-11-25 13:52:08 +01:00

Reworked UI using Fragments

This commit is contained in:
vanitasvitae 2016-09-28 21:43:25 +02:00
parent 2aaddec3a3
commit e4d4305fc1
15 changed files with 447 additions and 223 deletions

View file

@ -65,7 +65,7 @@ public class AboutActivity extends AppCompatActivity {
@BindView(R.id.main__topbar) @BindView(R.id.main__topbar)
protected Toolbar toolbar; protected Toolbar toolbar;
@BindView(R.id.linearlayout) @BindView(R.id.appbar_linear_layout)
protected LinearLayout linearLayout; protected LinearLayout linearLayout;
@Override @Override

View file

@ -62,8 +62,10 @@ 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.data.AppSettings; import com.github.dfa.diaspora_android.data.AppSettings;
import com.github.dfa.diaspora_android.data.PodUserProfile; import com.github.dfa.diaspora_android.data.PodUserProfile;
import com.github.dfa.diaspora_android.fragment.BrowserFragment;
import com.github.dfa.diaspora_android.fragment.CustomFragment; import com.github.dfa.diaspora_android.fragment.CustomFragment;
import com.github.dfa.diaspora_android.fragment.StreamFragment; import com.github.dfa.diaspora_android.fragment.DiasporaStreamFragment;
import com.github.dfa.diaspora_android.fragment.TestFragment;
import com.github.dfa.diaspora_android.listener.WebUserProfileChangedListener; import com.github.dfa.diaspora_android.listener.WebUserProfileChangedListener;
import com.github.dfa.diaspora_android.receiver.OpenExternalLinkReceiver; import com.github.dfa.diaspora_android.receiver.OpenExternalLinkReceiver;
import com.github.dfa.diaspora_android.receiver.UpdateTitleReceiver; import com.github.dfa.diaspora_android.receiver.UpdateTitleReceiver;
@ -162,10 +164,6 @@ public class MainActivity extends AppCompatActivity
customTabActivityHelper.setConnectionCallback(this); customTabActivityHelper.setConnectionCallback(this);
fm = getSupportFragmentManager(); fm = getSupportFragmentManager();
StreamFragment sf = getStreamFragment();
fm.beginTransaction().replace(R.id.fragment_container, sf, StreamFragment.TAG).commit();
sf.onCreateBottomOptionsMenu(toolbarBottom.getMenu(), getMenuInflater());
setupUI(savedInstanceState); setupUI(savedInstanceState);
brOpenExternalLink = new OpenExternalLinkReceiver(this); brOpenExternalLink = new OpenExternalLinkReceiver(this);
@ -180,7 +178,13 @@ public class MainActivity extends AppCompatActivity
MainActivity.this.setTitle(title); MainActivity.this.setTitle(title);
} }
}); });
handleIntent(getIntent()); //Handle intent
Intent intent = getIntent();
if(intent != null && intent.getAction() != null) {
handleIntent(intent);
} else {
openDiasporaUrl(urls.getStreamUrl());
}
} }
private void setupUI(Bundle savedInstanceState) { private void setupUI(Bundle savedInstanceState) {
@ -191,8 +195,8 @@ public class MainActivity extends AppCompatActivity
getMenuInflater().inflate(R.menu.main__menu_bottom, toolbarBottom.getMenu()); getMenuInflater().inflate(R.menu.main__menu_bottom, toolbarBottom.getMenu());
toolbarBottom.setOnMenuItemClickListener(new ActionMenuView.OnMenuItemClickListener() { toolbarBottom.setOnMenuItemClickListener(new ActionMenuView.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) { public boolean onMenuItemClick(MenuItem item) {
CustomFragment topFrag = (CustomFragment) getTopFragment(); CustomFragment top = getTopFragment();
return MainActivity.this.onOptionsItemSelected(item) || (topFrag != null && topFrag.onOptionsItemSelected(item)); return MainActivity.this.onOptionsItemSelected(item) || (top != null && top.onOptionsItemSelected(item));
} }
}); });
setTitle(R.string.app_name); setTitle(R.string.app_name);
@ -221,26 +225,64 @@ public class MainActivity extends AppCompatActivity
AppLog.v(this, "UI successfully set up"); AppLog.v(this, "UI successfully set up");
} }
public void openDiasporaUrl(String url) { /**
* Show DiasporaStreamFragment if necessary and load URL url
* @param url URL to load in the DiasporaStreamFragment
*/
protected void openDiasporaUrl(String url) {
AppLog.v(this, "openDiasporaUrl()"); AppLog.v(this, "openDiasporaUrl()");
StreamFragment streamFragment = getStreamFragment(); DiasporaStreamFragment streamFragment = (DiasporaStreamFragment) getFragment(DiasporaStreamFragment.TAG);
if(!streamFragment.isVisible()) { showFragment(streamFragment);
AppLog.d(this, "StreamFragment not visible");
fm.beginTransaction().replace(R.id.fragment_container, streamFragment, StreamFragment.TAG).commit();
streamFragment.onCreateBottomOptionsMenu(toolbarBottom.getMenu(), getMenuInflater());
}
streamFragment.loadUrl(url); streamFragment.loadUrl(url);
} }
public StreamFragment getStreamFragment() { /**
AppLog.v(this, "getStreamFragment()"); * Get an instance of the CustomFragment with the tag fragmentTag.
StreamFragment streamFragment = (StreamFragment) fm.findFragmentByTag(StreamFragment.TAG); * If there was no instance so far, create a new one and add it to the FragmentManagers pool.
if(streamFragment == null) { * If there is no Fragment with the corresponding Tag, return null.
AppLog.d(this, "StreamFragment was null"); * @param fragmentTag tag
streamFragment = new StreamFragment(); * @return corresponding Fragment
fm.beginTransaction().add(streamFragment, StreamFragment.TAG).commit(); */
protected CustomFragment getFragment(String fragmentTag) {
CustomFragment fragment = (CustomFragment) fm.findFragmentByTag(fragmentTag);
if(fragment != null) {
return fragment;
} else {
switch (fragmentTag) {
case DiasporaStreamFragment.TAG:
DiasporaStreamFragment dsf = new DiasporaStreamFragment();
fm.beginTransaction().add(dsf, fragmentTag).commit();
return dsf;
case BrowserFragment.TAG:
BrowserFragment bf = new BrowserFragment();
fm.beginTransaction().add(bf, fragmentTag).commit();
return bf;
case TestFragment.TAG:
TestFragment tf = new TestFragment();
fm.beginTransaction().add(tf, fragmentTag).commit();
return tf;
default:
AppLog.e(this,"Invalid Fragment Tag: "+fragmentTag
+"\nAdd Fragments Tag to getFragment()'s switch case.");
return null;
}
}
}
/**
* Show the Fragment fragment in R.id.fragment_container. If the fragment was already visible, do nothing.
* @param fragment Fragment to show
*/
protected void showFragment(CustomFragment fragment) {
AppLog.d(this, "showFragment()");
CustomFragment currentTop = (CustomFragment) fm.findFragmentById(R.id.fragment_container);
if(currentTop == null || !currentTop.getFragmentTag().equals(fragment.getFragmentTag())) {
AppLog.d(this, "Fragment was not visible. Replace it.");
fm.beginTransaction().addToBackStack(null).replace(R.id.fragment_container, fragment, fragment.getFragmentTag()).commit();
fragment.onCreateBottomOptionsMenu(toolbarBottom.getMenu(), getMenuInflater());
} else {
AppLog.d(this, "Fragment was already visible. Do nothing.");
} }
return streamFragment;
} }
@Override @Override
@ -307,7 +349,7 @@ public class MainActivity extends AppCompatActivity
navMenu.findItem(R.id.nav_aspects).setVisible(appSettings.isVisibleInNavAspects()); navMenu.findItem(R.id.nav_aspects).setVisible(appSettings.isVisibleInNavAspects());
navMenu.findItem(R.id.nav_commented).setVisible(appSettings.isVisibleInNavCommented()); navMenu.findItem(R.id.nav_commented).setVisible(appSettings.isVisibleInNavCommented());
navMenu.findItem(R.id.nav_followed_tags).setVisible(appSettings.isVisibleInNavFollowed_tags()); navMenu.findItem(R.id.nav_followed_tags).setVisible(appSettings.isVisibleInNavFollowed_tags());
navMenu.findItem(R.id.nav_help_license).setVisible(appSettings.isVisibleInNavHelp_license()); navMenu.findItem(R.id.nav_about).setVisible(appSettings.isVisibleInNavHelp_license());
navMenu.findItem(R.id.nav_liked).setVisible(appSettings.isVisibleInNavLiked()); navMenu.findItem(R.id.nav_liked).setVisible(appSettings.isVisibleInNavLiked());
navMenu.findItem(R.id.nav_mentions).setVisible(appSettings.isVisibleInNavMentions()); navMenu.findItem(R.id.nav_mentions).setVisible(appSettings.isVisibleInNavMentions());
navMenu.findItem(R.id.nav_profile).setVisible(appSettings.isVisibleInNavProfile()); navMenu.findItem(R.id.nav_profile).setVisible(appSettings.isVisibleInNavProfile());
@ -351,11 +393,11 @@ public class MainActivity extends AppCompatActivity
} }
} else if (ACTION_CHANGE_ACCOUNT.equals(action)) { } else if (ACTION_CHANGE_ACCOUNT.equals(action)) {
AppLog.v(this, "Reset pod data and animate to PodSelectionActivity"); AppLog.v(this, "Reset pod data and animate to PodSelectionActivity");
app.resetPodData(getStreamFragment().getWebView()); app.resetPodData(((DiasporaStreamFragment) getFragment(DiasporaStreamFragment.TAG)).getWebView());
Helpers.animateToActivity(MainActivity.this, PodSelectionActivity.class, true); Helpers.animateToActivity(MainActivity.this, PodSelectionActivity.class, true);
} else if (ACTION_CLEAR_CACHE.equals(action)) { } else if (ACTION_CLEAR_CACHE.equals(action)) {
AppLog.v(this, "Clear WebView cache"); AppLog.v(this, "Clear WebView cache");
getStreamFragment().getWebView().clearCache(true); ((DiasporaStreamFragment) getFragment(DiasporaStreamFragment.TAG)).getWebView().clearCache(true);
} else if (ACTION_RELOAD_ACTIVITY.equals(action)) { } else if (ACTION_RELOAD_ACTIVITY.equals(action)) {
AppLog.v(this, "Recreate activity"); AppLog.v(this, "Recreate activity");
recreate(); recreate();
@ -401,11 +443,10 @@ public class MainActivity extends AppCompatActivity
super.onRestoreInstanceState(savedInstanceState); super.onRestoreInstanceState(savedInstanceState);
} }
private Fragment getTopFragment() { private CustomFragment getTopFragment() {
for(Fragment f : fm.getFragments()) { Fragment top = fm.findFragmentById(R.id.fragment_container);
if(f.isVisible()) { if(top != null) {
return f; return (CustomFragment) top;
}
} }
return null; return null;
} }
@ -417,12 +458,17 @@ public class MainActivity extends AppCompatActivity
navDrawer.closeDrawer(navView); navDrawer.closeDrawer(navView);
return; return;
} }
CustomFragment top = (CustomFragment) getTopFragment(); CustomFragment top = getTopFragment();
if(top != null) { if(top != null) {
AppLog.v(this, "Top Fragment is not null"); AppLog.v(this, "Top Fragment is not null");
if(!top.onBackPressed()) { if(!top.onBackPressed()) {
AppLog.v(this, "Top Fragment.onBackPressed was false"); AppLog.v(this, "Top Fragment.onBackPressed was false");
snackbarExitApp.show(); AppLog.d(this, "BackStackEntryCount: "+fm.getBackStackEntryCount());
if(fm.getBackStackEntryCount()>0) {
fm.popBackStack();
} else {
snackbarExitApp.show();
}
return; return;
} else { } else {
AppLog.v(this, "Top Fragment.onBackPressed was true"); AppLog.v(this, "Top Fragment.onBackPressed was true");
@ -518,6 +564,11 @@ public class MainActivity extends AppCompatActivity
return true; return true;
} }
case R.id.action_debug_button: {
showFragment(getFragment(TestFragment.TAG));
return true;
}
case R.id.action_compose: { case R.id.action_compose: {
if (WebHelper.isOnline(MainActivity.this)) { if (WebHelper.isOnline(MainActivity.this)) {
openDiasporaUrl(urls.getNewPostUrl()); openDiasporaUrl(urls.getNewPostUrl());
@ -723,9 +774,10 @@ public class MainActivity extends AppCompatActivity
//TODO: Replace with fragment //TODO: Replace with fragment
case R.id.nav_followed_tags: { case R.id.nav_followed_tags: {
DiasporaStreamFragment stream = (DiasporaStreamFragment) getFragment(DiasporaStreamFragment.TAG);
if (WebHelper.isOnline(MainActivity.this)) { if (WebHelper.isOnline(MainActivity.this)) {
openDiasporaUrl(urls.getBlankUrl()); openDiasporaUrl(urls.getBlankUrl());
WebHelper.showFollowedTagsList(getStreamFragment().getWebView(), app); WebHelper.showFollowedTagsList(stream.getWebView(), app);
setTitle(R.string.nav_followed_tags); setTitle(R.string.nav_followed_tags);
} else { } else {
snackbarNoInternet.show(); snackbarNoInternet.show();
@ -735,9 +787,10 @@ public class MainActivity extends AppCompatActivity
//TODO: Replace with fragment //TODO: Replace with fragment
case R.id.nav_aspects: { case R.id.nav_aspects: {
DiasporaStreamFragment stream = (DiasporaStreamFragment) getFragment(DiasporaStreamFragment.TAG);
if (WebHelper.isOnline(MainActivity.this)) { if (WebHelper.isOnline(MainActivity.this)) {
openDiasporaUrl(DiasporaUrlHelper.URL_BLANK); openDiasporaUrl(DiasporaUrlHelper.URL_BLANK);
WebHelper.showAspectList(getStreamFragment().getWebView(), app); WebHelper.showAspectList(stream.getWebView(), app);
setTitle(R.string.aspects); setTitle(R.string.aspects);
} else { } else {
snackbarNoInternet.show(); snackbarNoInternet.show();
@ -801,7 +854,7 @@ public class MainActivity extends AppCompatActivity
} }
break; break;
case R.id.nav_help_license: { case R.id.nav_about: {
startActivity(new Intent(MainActivity.this, AboutActivity.class)); startActivity(new Intent(MainActivity.this, AboutActivity.class));
} }
break; break;

View file

@ -14,10 +14,13 @@ import android.os.Environment;
import android.os.StrictMode; import android.os.StrictMode;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings; import android.webkit.WebSettings;
import android.webkit.WebView; import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.Toast; import android.widget.Toast;
@ -27,6 +30,7 @@ import com.github.dfa.diaspora_android.activity.MainActivity;
import com.github.dfa.diaspora_android.data.AppSettings; import com.github.dfa.diaspora_android.data.AppSettings;
import com.github.dfa.diaspora_android.ui.ContextMenuWebView; import com.github.dfa.diaspora_android.ui.ContextMenuWebView;
import com.github.dfa.diaspora_android.ui.CustomWebViewClient; import com.github.dfa.diaspora_android.ui.CustomWebViewClient;
import com.github.dfa.diaspora_android.ui.ProgressBarWebChromeClient;
import com.github.dfa.diaspora_android.util.AppLog; import com.github.dfa.diaspora_android.util.AppLog;
import java.io.File; import java.io.File;
@ -42,29 +46,49 @@ import info.guardianproject.netcipher.NetCipher;
import info.guardianproject.netcipher.webkit.WebkitProxy; import info.guardianproject.netcipher.webkit.WebkitProxy;
/** /**
* Fragment that contains a WebView with a bunch of functionality * Fragment with a webView and a ProgressBar.
* Created by vanitas on 21.09.16. * This Fragment retains its instance.
* Created by vanitas on 26.09.16.
*/ */
public abstract class WebViewFragment extends CustomFragment { public class BrowserFragment extends CustomFragment {
public static final String TAG = "com.github.dfa.diaspora_android.BrowserFragment";
protected WebSettings webSettings; protected View rootLayout;
protected WebViewClient webViewClient;
protected ContextMenuWebView webView; protected ContextMenuWebView webView;
protected ProgressBar progressBar; protected ProgressBar progressBar;
protected AppSettings appSettings; protected AppSettings appSettings;
protected CustomWebViewClient webViewClient;
protected WebSettings webSettings;
protected String pendingUrl; protected String pendingUrl;
public void onViewCreated(View view, Bundle savedInstanceState) { @Override
super.onViewCreated(view, savedInstanceState); public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
AppLog.d(this, "onCreateView()");
if(rootLayout == null) {
rootLayout = inflater.inflate(R.layout.browser__fragment, container, false);
}
return rootLayout;
} }
protected void setup(ContextMenuWebView webView, final ProgressBar progressBar, AppSettings appSettings) { @Override
this.appSettings = appSettings; public void onViewCreated(View view, Bundle savedInstanceState) {
this.webSettings = webView.getSettings(); AppLog.d(this, "onViewCreated()");
this.webView = webView; super.onViewCreated(view, savedInstanceState);
this.progressBar = progressBar;
if(this.appSettings == null) {
this.appSettings = ((App) getActivity().getApplication()).getSettings();
}
if(this.webView == null) {
this.webView = (ContextMenuWebView) view.findViewById(R.id.webView);
this.applyWebViewSettings();
}
if(this.progressBar == null) {
this.progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
}
if (appSettings.isProxyEnabled()) { if (appSettings.isProxyEnabled()) {
if (!setProxy(appSettings.getProxyHost(), appSettings.getProxyPort())) { if (!setProxy(appSettings.getProxyHost(), appSettings.getProxyPort())) {
@ -75,7 +99,25 @@ public abstract class WebViewFragment extends CustomFragment {
resetProxy(); resetProxy();
} }
webSettings.setJavaScriptEnabled(true); if(pendingUrl != null) {
loadUrl(pendingUrl);
pendingUrl = null;
}
this.setRetainInstance(true);
}
@Override
public void onDestroyView() {
super.onDestroyView();
if (getRetainInstance() && rootLayout.getParent() instanceof ViewGroup) {
((ViewGroup) rootLayout.getParent()).removeView(rootLayout);
}
}
private void applyWebViewSettings() {
this.webSettings = webView.getSettings();
webSettings.setAllowFileAccess(false); webSettings.setAllowFileAccess(false);
webSettings.setUseWideViewPort(true); webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true); webSettings.setLoadWithOverviewMode(true);
@ -93,21 +135,9 @@ public abstract class WebViewFragment extends CustomFragment {
//webView.setParentActivity(this); //webView.setParentActivity(this);
webView.setOverScrollMode(WebView.OVER_SCROLL_ALWAYS); webView.setOverScrollMode(WebView.OVER_SCROLL_ALWAYS);
//Set proxy
if (appSettings.isProxyEnabled()) {
if (!setProxy(appSettings.getProxyHost(), appSettings.getProxyPort())) {
AppLog.d(this, "Could not enable Proxy");
Toast.makeText(getContext(), R.string.toast_set_proxy_failed, Toast.LENGTH_SHORT).show();
}
} else if (appSettings.wasProxyEnabled()) {
resetProxy();
}
/*
* WebViewClient
*/
this.webViewClient = new CustomWebViewClient((App) getActivity().getApplication(), webView); this.webViewClient = new CustomWebViewClient((App) getActivity().getApplication(), webView);
webView.setWebViewClient(webViewClient); webView.setWebViewClient(webViewClient);
webView.setWebChromeClient(new ProgressBarWebChromeClient(webView, progressBar));
} }
/** /**
@ -153,6 +183,7 @@ public abstract class WebViewFragment extends CustomFragment {
} }
} }
@SuppressWarnings("unused")
private boolean setProxy() { private boolean setProxy() {
return setProxy(appSettings.getProxyHost(), appSettings.getProxyPort()); return setProxy(appSettings.getProxyHost(), appSettings.getProxyPort());
} }
@ -175,7 +206,7 @@ public abstract class WebViewFragment extends CustomFragment {
AppLog.v(this, "clear WebKit proxy"); AppLog.v(this, "clear WebKit proxy");
WebkitProxy.resetProxy(MainActivity.class.getName(), getContext()); WebkitProxy.resetProxy(MainActivity.class.getName(), getContext());
} catch (Exception e) { } catch (Exception e) {
AppLog.e(this, "Could not clear WebKit proxy:\n"+e.toString()); AppLog.e(this, "Could not clear WebKit proxy:\n"+e.toString());
} }
AppLog.v(this, "Reset old ThreadPolicy"); AppLog.v(this, "Reset old ThreadPolicy");
StrictMode.setThreadPolicy(old); StrictMode.setThreadPolicy(old);
@ -272,6 +303,15 @@ public abstract class WebViewFragment extends CustomFragment {
} }
@Override @Override
public String getFragmentTag() {
return TAG;
}
@Override
public void onCreateBottomOptionsMenu(Menu menu, MenuInflater inflater) {
/* Nothing to do here */
}
public boolean onBackPressed() { public boolean onBackPressed() {
if(webView.canGoBack()) { if(webView.canGoBack()) {
webView.goBack(); webView.goBack();

View file

@ -12,6 +12,8 @@ import android.view.MenuInflater;
public abstract class CustomFragment extends Fragment { public abstract class CustomFragment extends Fragment {
public static final String TAG = "com.github.dfa.diaspora_android.CustomFragment";
/** /**
* We have an optionsMenu * We have an optionsMenu
* @param savedInstanceState state * @param savedInstanceState state

View file

@ -2,6 +2,7 @@ package com.github.dfa.diaspora_android.fragment;
import android.Manifest; import android.Manifest;
import android.animation.ObjectAnimator; import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
@ -12,23 +13,21 @@ import android.os.Bundle;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface; import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback; import android.webkit.ValueCallback;
import android.webkit.WebChromeClient; import android.webkit.WebChromeClient;
import android.webkit.WebView; import android.webkit.WebView;
import android.widget.ProgressBar;
import com.github.dfa.diaspora_android.App; 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.data.PodUserProfile; import com.github.dfa.diaspora_android.data.PodUserProfile;
import com.github.dfa.diaspora_android.ui.ContextMenuWebView; import com.github.dfa.diaspora_android.ui.DiasporaStreamWebChromeClient;
import com.github.dfa.diaspora_android.ui.FileUploadWebChromeClient;
import com.github.dfa.diaspora_android.util.AppLog; import com.github.dfa.diaspora_android.util.AppLog;
import com.github.dfa.diaspora_android.util.DiasporaUrlHelper; import com.github.dfa.diaspora_android.util.DiasporaUrlHelper;
import com.github.dfa.diaspora_android.util.Helpers; import com.github.dfa.diaspora_android.util.Helpers;
@ -40,167 +39,34 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
/** /**
* Fragment that contains a WebView displaying the stream of the user * Fragment that displays the Stream of the diaspora* user
* Created by vanitas on 21.09.16. * Created by vanitas on 26.09.16.
*/ */
public class StreamFragment extends WebViewFragment { public class DiasporaStreamFragment extends BrowserFragment {
public static final String TAG = "com.github.dfa.diaspora_android.StreamFragment"; public static final String TAG = "com.github.dfa.diaspora_android.StreamFragment";
private DiasporaUrlHelper urls; protected DiasporaUrlHelper urls;
private ValueCallback<Uri[]> imageUploadFilePathCallbackNew; private ValueCallback<Uri[]> imageUploadFilePathCallbackNew;
private ValueCallback<Uri> imageUploadFilePathCallbackOld; private ValueCallback<Uri> imageUploadFilePathCallbackOld;
private String mCameraPhotoPath; private String mCameraPhotoPath;
private String sharedText;
@Override @SuppressLint("SetJavaScriptEnabled")
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
AppLog.d(this, "onCreateView()");
return inflater.inflate(R.layout.stream__fragment, container, false);
}
@Override @Override
public void onViewCreated(View view, Bundle savedInstanceState) { public void onViewCreated(View view, Bundle savedInstanceState) {
AppLog.d(this, "onViewCreated()");
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
this.webView = (ContextMenuWebView) view.findViewById(R.id.webView);
this.progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
this.appSettings = ((App) getActivity().getApplication()).getSettings();
this.urls = new DiasporaUrlHelper(appSettings); this.urls = new DiasporaUrlHelper(appSettings);
webView.setWebChromeClient(new DiasporaStreamWebChromeClient(webView, progressBar, fileUploadCallback, sharedTextCallback));
this.setup( webView.getSettings().setJavaScriptEnabled(true);
webView,
progressBar,
appSettings);
// Setup WebView
webView.addJavascriptInterface(new JavaScriptInterface(), "AndroidBridge"); webView.addJavascriptInterface(new JavaScriptInterface(), "AndroidBridge");
setWebChromeClient(); if(((MainActivity)getActivity()).getTextToBeShared() != null) {
loadUrl(urls.getNewPostUrl());
if(pendingUrl != null) { } else if(webView.getUrl() == null) {
loadUrl(pendingUrl); loadUrl(urls.getStreamUrl());
pendingUrl = null;
} else if (webView.getUrl() == null) {
loadUrl(urls.getPodUrl());
} }
this.setRetainInstance(true);
}
private void setWebChromeClient() {
//Set WebChromeClient
webView.setWebChromeClient(new WebChromeClient() {
final ProgressBar pb = progressBar;
public void onProgressChanged(WebView wv, int progress) {
pb.setProgress(progress);
if (progress > 0 && progress <= 60) {
WebHelper.getUserProfile(wv);
WebHelper.optimizeMobileSiteLayout(wv);
}
if (progress > 60) {
WebHelper.optimizeMobileSiteLayout(wv);
String textToBeShared = ((MainActivity)getActivity()).getTextToBeShared();
if (textToBeShared != null) {
AppLog.d(this, "Share text into webView");
WebHelper.shareTextIntoWebView(wv, textToBeShared);
}
}
progressBar.setVisibility(progress == 100 ? View.GONE : View.VISIBLE);
}
//For Android 4.1/4.2 only. DO NOT REMOVE!
@SuppressWarnings("unused")
protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)
{
AppLog.v(this, "openFileChooser(ValCallback<Uri>, String, String");
imageUploadFilePathCallbackOld = uploadMsg;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("return-data", true);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
AppLog.v(this, "startActivityForResult");
startActivityForResult(Intent.createChooser(intent, "Select Picture"), MainActivity.INPUT_FILE_REQUEST_CODE_OLD);
}
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
if(Build.VERSION.SDK_INT >= 23) {
int hasWRITE_EXTERNAL_STORAGE = getActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (hasWRITE_EXTERNAL_STORAGE != PackageManager.PERMISSION_GRANTED) {
if (!shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
new AlertDialog.Builder(getContext())
.setMessage(R.string.permissions_image)
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (android.os.Build.VERSION.SDK_INT >= 23)
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MainActivity.REQUEST_CODE_ASK_PERMISSIONS);
}
})
.show();
return false;
}
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MainActivity.REQUEST_CODE_ASK_PERMISSIONS);
return false;
}
}
AppLog.d(this, "onOpenFileChooser");
if (imageUploadFilePathCallbackNew != null) imageUploadFilePathCallbackNew.onReceiveValue(null);
imageUploadFilePathCallbackNew = filePathCallback;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getContext().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile;
try {
photoFile = Helpers.createImageFile();
takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
} catch (IOException ex) {
AppLog.e(this, "ERROR creating temp file: "+ ex.toString());
// Error occurred while creating the File
Snackbar.make(webView, R.string.unable_to_load_image, Snackbar.LENGTH_LONG).show();
return false;
}
// Continue only if the File was successfully created
if (photoFile != null) {
mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
} else {
takePictureIntent = null;
}
}
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("image/*");
Intent[] intentArray;
if (takePictureIntent != null) {
intentArray = new Intent[]{takePictureIntent};
} else {
intentArray = new Intent[0];
}
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
AppLog.d(this, "startActivityForResult");
startActivityForResult(chooserIntent, MainActivity.INPUT_FILE_REQUEST_CODE_NEW);
return true;
}
});
}
@Override
public String getFragmentTag() {
return TAG;
} }
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
@ -273,12 +139,6 @@ public class StreamFragment extends WebViewFragment {
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
@Override
public ContextMenuWebView getWebView() {
AppLog.d(this, "getWebView: "+(this.webView != null));
return this.webView;
}
public void onImageUploadResult(int requestCode, int resultCode, Intent data) { public void onImageUploadResult(int requestCode, int resultCode, Intent data) {
AppLog.d(this, "onImageUploadResult"); AppLog.d(this, "onImageUploadResult");
switch (requestCode) { switch (requestCode) {
@ -346,7 +206,102 @@ public class StreamFragment extends WebViewFragment {
} }
} }
protected DiasporaStreamWebChromeClient.SharedTextCallback sharedTextCallback = new DiasporaStreamWebChromeClient.SharedTextCallback() {
@Override
public String getSharedText() {
return ((MainActivity)getActivity()).getTextToBeShared();
}
@Override
public void setSharedText(String shared) {
((MainActivity)getActivity()).setTextToBeShared(shared);
}
};
protected FileUploadWebChromeClient.FileUploadCallback fileUploadCallback = new FileUploadWebChromeClient.FileUploadCallback() {
@Override
public boolean imageUpload(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
if(Build.VERSION.SDK_INT >= 23) {
int hasWRITE_EXTERNAL_STORAGE = getActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (hasWRITE_EXTERNAL_STORAGE != PackageManager.PERMISSION_GRANTED) {
if (!shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
new AlertDialog.Builder(getContext())
.setMessage(R.string.permissions_image)
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (android.os.Build.VERSION.SDK_INT >= 23)
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MainActivity.REQUEST_CODE_ASK_PERMISSIONS);
}
})
.show();
return false;
}
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MainActivity.REQUEST_CODE_ASK_PERMISSIONS);
return false;
}
}
AppLog.d(this, "onOpenFileChooser");
if (imageUploadFilePathCallbackNew != null) imageUploadFilePathCallbackNew.onReceiveValue(null);
imageUploadFilePathCallbackNew = filePathCallback;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getContext().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile;
try {
photoFile = Helpers.createImageFile();
takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
} catch (IOException ex) {
AppLog.e(this, "ERROR creating temp file: "+ ex.toString());
// Error occurred while creating the File
Snackbar.make(webView, R.string.unable_to_load_image, Snackbar.LENGTH_LONG).show();
return false;
}
// Continue only if the File was successfully created
if (photoFile != null) {
mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
} else {
takePictureIntent = null;
}
}
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("image/*");
Intent[] intentArray;
if (takePictureIntent != null) {
intentArray = new Intent[]{takePictureIntent};
} else {
intentArray = new Intent[0];
}
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
AppLog.d(this, "startActivityForResult");
startActivityForResult(chooserIntent, MainActivity.INPUT_FILE_REQUEST_CODE_NEW);
return true;
}
@Override
public void legacyImageUpload(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
AppLog.v(this, "openFileChooser(ValCallback<Uri>, String, String");
imageUploadFilePathCallbackOld = uploadMsg;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("return-data", true);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
AppLog.v(this, "startActivityForResult");
startActivityForResult(Intent.createChooser(intent, "Select Picture"), MainActivity.INPUT_FILE_REQUEST_CODE_OLD);
}
};
private class JavaScriptInterface { private class JavaScriptInterface {
@SuppressWarnings("unused")
@JavascriptInterface @JavascriptInterface
public void setUserProfile(final String webMessage) throws JSONException { public void setUserProfile(final String webMessage) throws JSONException {
PodUserProfile pup = ((App)getActivity().getApplication()).getPodUserProfile(); PodUserProfile pup = ((App)getActivity().getApplication()).getPodUserProfile();
@ -359,9 +314,15 @@ public class StreamFragment extends WebViewFragment {
} }
} }
@SuppressWarnings("unused")
@JavascriptInterface @JavascriptInterface
public void contentHasBeenShared() { public void contentHasBeenShared() {
((MainActivity)getActivity()).setTextToBeShared(null); ((MainActivity)getActivity()).setTextToBeShared(null);
} }
} }
@Override
public String getFragmentTag() {
return TAG;
}
} }

View file

@ -0,0 +1,39 @@
package com.github.dfa.diaspora_android.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import com.github.dfa.diaspora_android.R;
import com.github.dfa.diaspora_android.util.AppLog;
/**
* Created by vanitas on 23.09.16.
*/
public class TestFragment extends CustomFragment {
public static final String TAG = "com.github.dfa.diaspora_android.TestFragment";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
AppLog.d(this, "onCreateView()");
return inflater.inflate(R.layout.test__fragment, container, false);
}
@Override
public String getFragmentTag() {
return TAG;
}
@Override
public void onCreateBottomOptionsMenu(Menu menu, MenuInflater inflater) {
}
@Override
public boolean onBackPressed() {
return false;
}
}

View file

@ -0,0 +1,44 @@
package com.github.dfa.diaspora_android.ui;
import android.webkit.WebView;
import android.widget.ProgressBar;
import com.github.dfa.diaspora_android.util.AppLog;
import com.github.dfa.diaspora_android.util.WebHelper;
/**
* Created by vanitas on 26.09.16.
*/
public class DiasporaStreamWebChromeClient extends FileUploadWebChromeClient {
protected SharedTextCallback sharedTextCallback;
public DiasporaStreamWebChromeClient(WebView webView, ProgressBar progressBar, FileUploadCallback fileUploadCallback, SharedTextCallback callback) {
super(webView, progressBar, fileUploadCallback);
this.sharedTextCallback = callback;
}
@Override
public void onProgressChanged(WebView wv, int progress) {
super.onProgressChanged(wv, progress);
if (progress > 0 && progress <= 60) {
WebHelper.getUserProfile(wv);
WebHelper.optimizeMobileSiteLayout(wv);
}
if (progress > 60) {
WebHelper.optimizeMobileSiteLayout(wv);
String textToBeShared = sharedTextCallback.getSharedText();
if (textToBeShared != null) {
AppLog.d(this, "Share text into webView");
WebHelper.shareTextIntoWebView(wv, textToBeShared);
}
}
}
public interface SharedTextCallback {
String getSharedText();
void setSharedText(String shared);
}
}

View file

@ -0,0 +1,41 @@
package com.github.dfa.diaspora_android.ui;
import android.net.Uri;
import android.webkit.ValueCallback;
import android.webkit.WebView;
import android.widget.ProgressBar;
/**
* Created by vanitas on 26.09.16.
*/
public class FileUploadWebChromeClient extends ProgressBarWebChromeClient {
protected FileUploadCallback fileUploadCallback;
public FileUploadWebChromeClient(WebView webView, ProgressBar progressBar, FileUploadCallback fileUploadCallback) {
super(webView, progressBar);
this.fileUploadCallback = fileUploadCallback;
}
@Override
public void onProgressChanged(WebView wv, int progress) {
super.onProgressChanged(wv, progress);
}
//For Android 4.1/4.2 only. DO NOT REMOVE!
@SuppressWarnings("unused")
protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)
{
fileUploadCallback.legacyImageUpload(uploadMsg, acceptType, capture);
}
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
return fileUploadCallback.imageUpload(webView, filePathCallback, fileChooserParams);
}
public interface FileUploadCallback {
boolean imageUpload(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams);
void legacyImageUpload(ValueCallback<Uri> uploadMsg, String acceptType, String capture);
}
}

View file

@ -0,0 +1,26 @@
package com.github.dfa.diaspora_android.ui;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ProgressBar;
/**
* WebChromeClient that connects the ProgressBar and the WebView and updates the progress of the progressBar.
* Created by vanitas on 26.09.16.
*/
public class ProgressBarWebChromeClient extends WebChromeClient {
protected final ProgressBar progressBar;
protected final WebView webView;
public ProgressBarWebChromeClient(WebView webView, ProgressBar progressBar) {
this.webView = webView;
this.progressBar = progressBar;
}
public void onProgressChanged(WebView wv, int progress) {
progressBar.setProgress(progress);
progressBar.setVisibility(progress == 100 ? View.GONE : View.VISIBLE);
}
}

View file

@ -62,7 +62,7 @@ public class WebHelper {
public static void optimizeMobileSiteLayout(final WebView wv) { public static void optimizeMobileSiteLayout(final WebView wv) {
wv.loadUrl("javascript: ( function() {" + wv.loadUrl("javascript: ( function() {" +
" if (document.documentElement == null || document.documentElement.style == null) { return; }" + " if (document.documentElement == null || document.documentElement.style == null) { return; }" +
" document.documentElement.style.paddingBottom = '260px';" + " document.documentElement.style.paddingBottom = '50px';" +
" document.getElementById('main').style.paddingTop = '5px';" + " document.getElementById('main').style.paddingTop = '5px';" +
" if(document.getElementById('main_nav')) {" + " if(document.getElementById('main_nav')) {" +
" document.getElementById('main_nav').parentNode.removeChild(" + " document.getElementById('main_nav').parentNode.removeChild(" +

View file

@ -17,7 +17,7 @@
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/linearlayout" android:id="@+id/appbar_linear_layout"
android:orientation="vertical" android:orientation="vertical"
android:background="?attr/colorPrimary" android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|snap" app:layout_scrollFlags="scroll|enterAlways|snap"

View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lorem_ipsum"/>
</LinearLayout>

View file

@ -35,4 +35,11 @@
android:title="@string/action_exit_app" android:title="@string/action_exit_app"
/> />
<item
android:id="@+id/action_debug_button"
app:showAsAction="never"
android:orderInCategory="1000"
android:title="DEBUG"
/>
</menu> </menu>

View file

@ -61,7 +61,7 @@
android:title="@string/settings" /> android:title="@string/settings" />
<item <item
android:id="@+id/nav_help_license" android:id="@+id/nav_about"
android:icon="@drawable/ic_info_black_48px" android:icon="@drawable/ic_info_black_48px"
android:title="@string/nav_help_license" /> android:title="@string/nav_help_license" />
</menu> </menu>