diff --git a/app/src/main/java/com/github/dfa/diaspora_android/activity/AboutActivity.java b/app/src/main/java/com/github/dfa/diaspora_android/activity/AboutActivity.java index 4d8e2ecd..cc9cf36d 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/activity/AboutActivity.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/activity/AboutActivity.java @@ -65,7 +65,7 @@ public class AboutActivity extends AppCompatActivity { @BindView(R.id.main__topbar) protected Toolbar toolbar; - @BindView(R.id.linearlayout) + @BindView(R.id.appbar_linear_layout) protected LinearLayout linearLayout; @Override diff --git a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java index 19add8dd..244bbc05 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java @@ -62,8 +62,10 @@ import com.github.dfa.diaspora_android.App; import com.github.dfa.diaspora_android.R; import com.github.dfa.diaspora_android.data.AppSettings; 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.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.receiver.OpenExternalLinkReceiver; import com.github.dfa.diaspora_android.receiver.UpdateTitleReceiver; @@ -162,10 +164,6 @@ public class MainActivity extends AppCompatActivity customTabActivityHelper.setConnectionCallback(this); fm = getSupportFragmentManager(); - StreamFragment sf = getStreamFragment(); - fm.beginTransaction().replace(R.id.fragment_container, sf, StreamFragment.TAG).commit(); - sf.onCreateBottomOptionsMenu(toolbarBottom.getMenu(), getMenuInflater()); - setupUI(savedInstanceState); brOpenExternalLink = new OpenExternalLinkReceiver(this); @@ -180,7 +178,13 @@ public class MainActivity extends AppCompatActivity 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) { @@ -191,8 +195,8 @@ public class MainActivity extends AppCompatActivity getMenuInflater().inflate(R.menu.main__menu_bottom, toolbarBottom.getMenu()); toolbarBottom.setOnMenuItemClickListener(new ActionMenuView.OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { - CustomFragment topFrag = (CustomFragment) getTopFragment(); - return MainActivity.this.onOptionsItemSelected(item) || (topFrag != null && topFrag.onOptionsItemSelected(item)); + CustomFragment top = getTopFragment(); + return MainActivity.this.onOptionsItemSelected(item) || (top != null && top.onOptionsItemSelected(item)); } }); setTitle(R.string.app_name); @@ -221,26 +225,64 @@ public class MainActivity extends AppCompatActivity 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()"); - StreamFragment streamFragment = getStreamFragment(); - if(!streamFragment.isVisible()) { - AppLog.d(this, "StreamFragment not visible"); - fm.beginTransaction().replace(R.id.fragment_container, streamFragment, StreamFragment.TAG).commit(); - streamFragment.onCreateBottomOptionsMenu(toolbarBottom.getMenu(), getMenuInflater()); - } + DiasporaStreamFragment streamFragment = (DiasporaStreamFragment) getFragment(DiasporaStreamFragment.TAG); + showFragment(streamFragment); streamFragment.loadUrl(url); } - public StreamFragment getStreamFragment() { - AppLog.v(this, "getStreamFragment()"); - StreamFragment streamFragment = (StreamFragment) fm.findFragmentByTag(StreamFragment.TAG); - if(streamFragment == null) { - AppLog.d(this, "StreamFragment was null"); - streamFragment = new StreamFragment(); - fm.beginTransaction().add(streamFragment, StreamFragment.TAG).commit(); + /** + * 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. + * If there is no Fragment with the corresponding Tag, return null. + * @param fragmentTag tag + * @return corresponding Fragment + */ + 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 @@ -307,7 +349,7 @@ public class MainActivity extends AppCompatActivity navMenu.findItem(R.id.nav_aspects).setVisible(appSettings.isVisibleInNavAspects()); 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_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_mentions).setVisible(appSettings.isVisibleInNavMentions()); 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)) { 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); } else if (ACTION_CLEAR_CACHE.equals(action)) { AppLog.v(this, "Clear WebView cache"); - getStreamFragment().getWebView().clearCache(true); + ((DiasporaStreamFragment) getFragment(DiasporaStreamFragment.TAG)).getWebView().clearCache(true); } else if (ACTION_RELOAD_ACTIVITY.equals(action)) { AppLog.v(this, "Recreate activity"); recreate(); @@ -401,11 +443,10 @@ public class MainActivity extends AppCompatActivity super.onRestoreInstanceState(savedInstanceState); } - private Fragment getTopFragment() { - for(Fragment f : fm.getFragments()) { - if(f.isVisible()) { - return f; - } + private CustomFragment getTopFragment() { + Fragment top = fm.findFragmentById(R.id.fragment_container); + if(top != null) { + return (CustomFragment) top; } return null; } @@ -417,12 +458,17 @@ public class MainActivity extends AppCompatActivity navDrawer.closeDrawer(navView); return; } - CustomFragment top = (CustomFragment) getTopFragment(); + CustomFragment top = getTopFragment(); if(top != null) { AppLog.v(this, "Top Fragment is not null"); if(!top.onBackPressed()) { 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; } else { AppLog.v(this, "Top Fragment.onBackPressed was true"); @@ -518,6 +564,11 @@ public class MainActivity extends AppCompatActivity return true; } + case R.id.action_debug_button: { + showFragment(getFragment(TestFragment.TAG)); + return true; + } + case R.id.action_compose: { if (WebHelper.isOnline(MainActivity.this)) { openDiasporaUrl(urls.getNewPostUrl()); @@ -723,9 +774,10 @@ public class MainActivity extends AppCompatActivity //TODO: Replace with fragment case R.id.nav_followed_tags: { + DiasporaStreamFragment stream = (DiasporaStreamFragment) getFragment(DiasporaStreamFragment.TAG); if (WebHelper.isOnline(MainActivity.this)) { openDiasporaUrl(urls.getBlankUrl()); - WebHelper.showFollowedTagsList(getStreamFragment().getWebView(), app); + WebHelper.showFollowedTagsList(stream.getWebView(), app); setTitle(R.string.nav_followed_tags); } else { snackbarNoInternet.show(); @@ -735,9 +787,10 @@ public class MainActivity extends AppCompatActivity //TODO: Replace with fragment case R.id.nav_aspects: { + DiasporaStreamFragment stream = (DiasporaStreamFragment) getFragment(DiasporaStreamFragment.TAG); if (WebHelper.isOnline(MainActivity.this)) { openDiasporaUrl(DiasporaUrlHelper.URL_BLANK); - WebHelper.showAspectList(getStreamFragment().getWebView(), app); + WebHelper.showAspectList(stream.getWebView(), app); setTitle(R.string.aspects); } else { snackbarNoInternet.show(); @@ -801,7 +854,7 @@ public class MainActivity extends AppCompatActivity } break; - case R.id.nav_help_license: { + case R.id.nav_about: { startActivity(new Intent(MainActivity.this, AboutActivity.class)); } break; diff --git a/app/src/main/java/com/github/dfa/diaspora_android/fragment/WebViewFragment.java b/app/src/main/java/com/github/dfa/diaspora_android/fragment/BrowserFragment.java similarity index 84% rename from app/src/main/java/com/github/dfa/diaspora_android/fragment/WebViewFragment.java rename to app/src/main/java/com/github/dfa/diaspora_android/fragment/BrowserFragment.java index 7127e427..01fdb390 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/fragment/WebViewFragment.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/fragment/BrowserFragment.java @@ -14,10 +14,13 @@ import android.os.Environment; import android.os.StrictMode; import android.support.design.widget.Snackbar; 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.ViewGroup; import android.webkit.WebSettings; import android.webkit.WebView; -import android.webkit.WebViewClient; import android.widget.ProgressBar; 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.ui.ContextMenuWebView; 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 java.io.File; @@ -42,29 +46,49 @@ import info.guardianproject.netcipher.NetCipher; import info.guardianproject.netcipher.webkit.WebkitProxy; /** - * Fragment that contains a WebView with a bunch of functionality - * Created by vanitas on 21.09.16. + * Fragment with a webView and a ProgressBar. + * 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 WebViewClient webViewClient; + protected View rootLayout; protected ContextMenuWebView webView; protected ProgressBar progressBar; protected AppSettings appSettings; + protected CustomWebViewClient webViewClient; + protected WebSettings webSettings; protected String pendingUrl; - public void onViewCreated(View view, Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); + @Override + 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) { - this.appSettings = appSettings; - this.webSettings = webView.getSettings(); - this.webView = webView; - this.progressBar = progressBar; + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + AppLog.d(this, "onViewCreated()"); + super.onViewCreated(view, savedInstanceState); + + 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 (!setProxy(appSettings.getProxyHost(), appSettings.getProxyPort())) { @@ -75,7 +99,25 @@ public abstract class WebViewFragment extends CustomFragment { 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.setUseWideViewPort(true); webSettings.setLoadWithOverviewMode(true); @@ -93,21 +135,9 @@ public abstract class WebViewFragment extends CustomFragment { //webView.setParentActivity(this); 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); webView.setWebViewClient(webViewClient); + webView.setWebChromeClient(new ProgressBarWebChromeClient(webView, progressBar)); } /** @@ -153,6 +183,7 @@ public abstract class WebViewFragment extends CustomFragment { } } + @SuppressWarnings("unused") private boolean setProxy() { return setProxy(appSettings.getProxyHost(), appSettings.getProxyPort()); } @@ -175,7 +206,7 @@ public abstract class WebViewFragment extends CustomFragment { AppLog.v(this, "clear WebKit proxy"); WebkitProxy.resetProxy(MainActivity.class.getName(), getContext()); } 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"); StrictMode.setThreadPolicy(old); @@ -272,6 +303,15 @@ public abstract class WebViewFragment extends CustomFragment { } @Override + public String getFragmentTag() { + return TAG; + } + + @Override + public void onCreateBottomOptionsMenu(Menu menu, MenuInflater inflater) { + /* Nothing to do here */ + } + public boolean onBackPressed() { if(webView.canGoBack()) { webView.goBack(); diff --git a/app/src/main/java/com/github/dfa/diaspora_android/fragment/CustomFragment.java b/app/src/main/java/com/github/dfa/diaspora_android/fragment/CustomFragment.java index 255370cc..5b833e37 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/fragment/CustomFragment.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/fragment/CustomFragment.java @@ -12,6 +12,8 @@ import android.view.MenuInflater; public abstract class CustomFragment extends Fragment { + public static final String TAG = "com.github.dfa.diaspora_android.CustomFragment"; + /** * We have an optionsMenu * @param savedInstanceState state diff --git a/app/src/main/java/com/github/dfa/diaspora_android/fragment/StreamFragment.java b/app/src/main/java/com/github/dfa/diaspora_android/fragment/DiasporaStreamFragment.java similarity index 54% rename from app/src/main/java/com/github/dfa/diaspora_android/fragment/StreamFragment.java rename to app/src/main/java/com/github/dfa/diaspora_android/fragment/DiasporaStreamFragment.java index 90dba5f8..8a86243c 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/fragment/StreamFragment.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/fragment/DiasporaStreamFragment.java @@ -2,6 +2,7 @@ package com.github.dfa.diaspora_android.fragment; import android.Manifest; import android.animation.ObjectAnimator; +import android.annotation.SuppressLint; import android.app.Activity; import android.content.DialogInterface; import android.content.Intent; @@ -12,23 +13,21 @@ import android.os.Bundle; import android.provider.MediaStore; import android.support.design.widget.Snackbar; import android.support.v7.app.AlertDialog; -import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; -import android.view.ViewGroup; import android.webkit.JavascriptInterface; import android.webkit.ValueCallback; import android.webkit.WebChromeClient; import android.webkit.WebView; -import android.widget.ProgressBar; import com.github.dfa.diaspora_android.App; import com.github.dfa.diaspora_android.R; import com.github.dfa.diaspora_android.activity.MainActivity; 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.DiasporaUrlHelper; import com.github.dfa.diaspora_android.util.Helpers; @@ -40,167 +39,34 @@ import java.io.File; import java.io.IOException; /** - * Fragment that contains a WebView displaying the stream of the user - * Created by vanitas on 21.09.16. + * Fragment that displays the Stream of the diaspora* user + * 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"; - private DiasporaUrlHelper urls; + protected DiasporaUrlHelper urls; private ValueCallback imageUploadFilePathCallbackNew; private ValueCallback imageUploadFilePathCallbackOld; private String mCameraPhotoPath; + private String sharedText; - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - AppLog.d(this, "onCreateView()"); - return inflater.inflate(R.layout.stream__fragment, container, false); - } - + @SuppressLint("SetJavaScriptEnabled") @Override public void onViewCreated(View view, Bundle savedInstanceState) { - AppLog.d(this, "onViewCreated()"); 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); + webView.setWebChromeClient(new DiasporaStreamWebChromeClient(webView, progressBar, fileUploadCallback, sharedTextCallback)); - this.setup( - webView, - progressBar, - appSettings); - - // Setup WebView + webView.getSettings().setJavaScriptEnabled(true); webView.addJavascriptInterface(new JavaScriptInterface(), "AndroidBridge"); - setWebChromeClient(); - - if(pendingUrl != null) { - loadUrl(pendingUrl); - pendingUrl = null; - } else if (webView.getUrl() == null) { - loadUrl(urls.getPodUrl()); + if(((MainActivity)getActivity()).getTextToBeShared() != null) { + loadUrl(urls.getNewPostUrl()); + } else if(webView.getUrl() == null) { + loadUrl(urls.getStreamUrl()); } - - 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 uploadMsg, String acceptType, String capture) - { - AppLog.v(this, "openFileChooser(ValCallback, 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 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) { @@ -273,12 +139,6 @@ public class StreamFragment extends WebViewFragment { 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) { AppLog.d(this, "onImageUploadResult"); 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 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 uploadMsg, String acceptType, String capture) { + AppLog.v(this, "openFileChooser(ValCallback, 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 { + @SuppressWarnings("unused") @JavascriptInterface public void setUserProfile(final String webMessage) throws JSONException { PodUserProfile pup = ((App)getActivity().getApplication()).getPodUserProfile(); @@ -359,9 +314,15 @@ public class StreamFragment extends WebViewFragment { } } + @SuppressWarnings("unused") @JavascriptInterface public void contentHasBeenShared() { ((MainActivity)getActivity()).setTextToBeShared(null); } } + + @Override + public String getFragmentTag() { + return TAG; + } } diff --git a/app/src/main/java/com/github/dfa/diaspora_android/fragment/TestFragment.java b/app/src/main/java/com/github/dfa/diaspora_android/fragment/TestFragment.java new file mode 100644 index 00000000..c8d5a861 --- /dev/null +++ b/app/src/main/java/com/github/dfa/diaspora_android/fragment/TestFragment.java @@ -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; + } +} diff --git a/app/src/main/java/com/github/dfa/diaspora_android/ui/DiasporaStreamWebChromeClient.java b/app/src/main/java/com/github/dfa/diaspora_android/ui/DiasporaStreamWebChromeClient.java new file mode 100644 index 00000000..671075b2 --- /dev/null +++ b/app/src/main/java/com/github/dfa/diaspora_android/ui/DiasporaStreamWebChromeClient.java @@ -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); + } +} diff --git a/app/src/main/java/com/github/dfa/diaspora_android/ui/FileUploadWebChromeClient.java b/app/src/main/java/com/github/dfa/diaspora_android/ui/FileUploadWebChromeClient.java new file mode 100644 index 00000000..6e3172e2 --- /dev/null +++ b/app/src/main/java/com/github/dfa/diaspora_android/ui/FileUploadWebChromeClient.java @@ -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 uploadMsg, String acceptType, String capture) + { + fileUploadCallback.legacyImageUpload(uploadMsg, acceptType, capture); + } + + @Override + public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams) { + return fileUploadCallback.imageUpload(webView, filePathCallback, fileChooserParams); + } + + public interface FileUploadCallback { + boolean imageUpload(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams); + void legacyImageUpload(ValueCallback uploadMsg, String acceptType, String capture); + } +} diff --git a/app/src/main/java/com/github/dfa/diaspora_android/ui/ProgressBarWebChromeClient.java b/app/src/main/java/com/github/dfa/diaspora_android/ui/ProgressBarWebChromeClient.java new file mode 100644 index 00000000..1c8e84c3 --- /dev/null +++ b/app/src/main/java/com/github/dfa/diaspora_android/ui/ProgressBarWebChromeClient.java @@ -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); + } +} diff --git a/app/src/main/java/com/github/dfa/diaspora_android/util/WebHelper.java b/app/src/main/java/com/github/dfa/diaspora_android/util/WebHelper.java index 691481be..ba760d7b 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/util/WebHelper.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/util/WebHelper.java @@ -62,7 +62,7 @@ public class WebHelper { public static void optimizeMobileSiteLayout(final WebView wv) { wv.loadUrl("javascript: ( function() {" + " 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';" + " if(document.getElementById('main_nav')) {" + " document.getElementById('main_nav').parentNode.removeChild(" + diff --git a/app/src/main/res/layout/about__activity.xml b/app/src/main/res/layout/about__activity.xml index 34de3bfd..6a8c3568 100644 --- a/app/src/main/res/layout/about__activity.xml +++ b/app/src/main/res/layout/about__activity.xml @@ -17,7 +17,7 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/main__menu_bottom.xml b/app/src/main/res/menu/main__menu_bottom.xml index 0c973856..063957cf 100644 --- a/app/src/main/res/menu/main__menu_bottom.xml +++ b/app/src/main/res/menu/main__menu_bottom.xml @@ -35,4 +35,11 @@ android:title="@string/action_exit_app" /> + + diff --git a/app/src/main/res/menu/main__navdrawer.xml b/app/src/main/res/menu/main__navdrawer.xml index 25a2f7a9..56323eb8 100644 --- a/app/src/main/res/menu/main__navdrawer.xml +++ b/app/src/main/res/menu/main__navdrawer.xml @@ -61,7 +61,7 @@ android:title="@string/settings" />