mirror of
https://github.com/gsantner/dandelion
synced 2024-12-22 19:08:03 +01:00
Moved some code from WebViewFragment to StreamFragment and vice versa
This commit is contained in:
commit
3eb5b2c867
4 changed files with 283 additions and 261 deletions
|
@ -18,13 +18,7 @@
|
|||
*/
|
||||
package com.github.dfa.diaspora_android.activity;
|
||||
|
||||
import android.Manifest;
|
||||
import android.animation.ObjectAnimator;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.app.AlarmManager;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
|
@ -32,16 +26,12 @@ import android.content.Intent;
|
|||
import android.content.IntentFilter;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.res.Configuration;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.drawable.LayerDrawable;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.os.Handler;
|
||||
import android.os.StrictMode;
|
||||
import android.provider.MediaStore;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.customtabs.CustomTabsSession;
|
||||
import android.support.design.widget.AppBarLayout;
|
||||
import android.support.design.widget.NavigationView;
|
||||
import android.support.design.widget.Snackbar;
|
||||
|
@ -61,16 +51,10 @@ import android.view.View;
|
|||
import android.view.WindowManager;
|
||||
import android.view.inputmethod.EditorInfo;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
import android.webkit.JavascriptInterface;
|
||||
import android.webkit.ValueCallback;
|
||||
import android.webkit.WebChromeClient;
|
||||
import android.webkit.WebSettings;
|
||||
import android.webkit.WebView;
|
||||
import android.widget.EditText;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
|
@ -84,39 +68,24 @@ import com.github.dfa.diaspora_android.listener.WebUserProfileChangedListener;
|
|||
import com.github.dfa.diaspora_android.receiver.OpenExternalLinkReceiver;
|
||||
import com.github.dfa.diaspora_android.receiver.UpdateTitleReceiver;
|
||||
import com.github.dfa.diaspora_android.ui.BadgeDrawable;
|
||||
import com.github.dfa.diaspora_android.ui.ContextMenuWebView;
|
||||
import com.github.dfa.diaspora_android.ui.CustomWebViewClient;
|
||||
import com.github.dfa.diaspora_android.util.AppLog;
|
||||
import com.github.dfa.diaspora_android.util.CustomTabHelpers.CustomTabActivityHelper;
|
||||
import com.github.dfa.diaspora_android.util.DiasporaUrlHelper;
|
||||
import com.github.dfa.diaspora_android.util.Helpers;
|
||||
import com.github.dfa.diaspora_android.util.WebHelper;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
import info.guardianproject.netcipher.NetCipher;
|
||||
import info.guardianproject.netcipher.webkit.WebkitProxy;
|
||||
|
||||
public class MainActivity extends AppCompatActivity
|
||||
implements NavigationView.OnNavigationItemSelectedListener, WebUserProfileChangedListener {
|
||||
implements NavigationView.OnNavigationItemSelectedListener, WebUserProfileChangedListener, CustomTabActivityHelper.ConnectionCallback {
|
||||
|
||||
|
||||
public static final int INPUT_FILE_REQUEST_CODE_NEW = 1;
|
||||
public static final int INPUT_FILE_REQUEST_CODE_OLD = 2;
|
||||
public static final int REQUEST_CODE_ASK_PERMISSIONS = 123;
|
||||
public static final int REQUEST_CODE__ACCESS_EXTERNAL_STORAGE = 124;
|
||||
public static final int INPUT_FILE_REQUEST_CODE_NEW = 1;
|
||||
public static final int INPUT_FILE_REQUEST_CODE_OLD = 2;
|
||||
|
||||
public static final String ACTION_OPEN_URL = "com.github.dfa.diaspora_android.MainActivity.open_url";
|
||||
public static final String ACTION_OPEN_EXTERNAL_URL = "com.github.dfa.diaspora_android.MainActivity.open_external_url";
|
||||
|
@ -129,9 +98,6 @@ public class MainActivity extends AppCompatActivity
|
|||
public static final String CONTENT_HASHTAG = "content://com.github.dfa.diaspora_android.mainactivity/";
|
||||
|
||||
private App app;
|
||||
private ValueCallback<Uri[]> imageUploadFilePathCallbackNew;
|
||||
private ValueCallback<Uri> imageUploadFilePathCallbackOld;
|
||||
private String mCameraPhotoPath;
|
||||
private CustomTabActivityHelper customTabActivityHelper;
|
||||
private AppSettings appSettings;
|
||||
private DiasporaUrlHelper urls;
|
||||
|
@ -141,8 +107,8 @@ public class MainActivity extends AppCompatActivity
|
|||
private BroadcastReceiver brSetTitle;
|
||||
private Snackbar snackbarExitApp;
|
||||
private Snackbar snackbarNoInternet;
|
||||
|
||||
private FragmentManager fm;
|
||||
private CustomTabsSession customTabsSession;
|
||||
|
||||
/**
|
||||
* UI Bindings
|
||||
|
@ -191,6 +157,7 @@ public class MainActivity extends AppCompatActivity
|
|||
podUserProfile.setListener(this);
|
||||
urls = new DiasporaUrlHelper(appSettings);
|
||||
customTabActivityHelper = new CustomTabActivityHelper();
|
||||
customTabActivityHelper.setConnectionCallback(this);
|
||||
|
||||
fm = getSupportFragmentManager();
|
||||
StreamFragment sf = getStreamFragment();
|
||||
|
@ -411,70 +378,7 @@ public class MainActivity extends AppCompatActivity
|
|||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
AppLog.v(this, "onActivityResult()");
|
||||
switch (requestCode) {
|
||||
case INPUT_FILE_REQUEST_CODE_NEW: {
|
||||
AppLog.v(this, "Upload image using recent method (Lollipop+)");
|
||||
if (imageUploadFilePathCallbackNew == null || resultCode != Activity.RESULT_OK) {
|
||||
AppLog.e(this, "Callback is null: " + (imageUploadFilePathCallbackNew == null)
|
||||
+ " resultCode: " + resultCode);
|
||||
if(imageUploadFilePathCallbackNew != null)
|
||||
imageUploadFilePathCallbackNew.onReceiveValue(new Uri[]{});
|
||||
return;
|
||||
}
|
||||
Uri[] results = null;
|
||||
if (data == null) {
|
||||
if (mCameraPhotoPath != null) {
|
||||
AppLog.v(this, "Intent data is null. Try to parse cameraPhotoPath");
|
||||
results = new Uri[]{Uri.parse(mCameraPhotoPath)};
|
||||
} else {
|
||||
AppLog.w(this, "Intent data is null and cameraPhotoPath is null");
|
||||
}
|
||||
} else {
|
||||
String dataString = data.getDataString();
|
||||
if (dataString != null) {
|
||||
AppLog.v(this, "Intent has data. Try to parse dataString");
|
||||
results = new Uri[]{Uri.parse(dataString)};
|
||||
}
|
||||
AppLog.w(this, "dataString is null");
|
||||
}
|
||||
AppLog.v(this, "handle received result over to callback");
|
||||
imageUploadFilePathCallbackNew.onReceiveValue(results);
|
||||
imageUploadFilePathCallbackNew = null;
|
||||
return;
|
||||
}
|
||||
case INPUT_FILE_REQUEST_CODE_OLD: {
|
||||
AppLog.v(this, "Upload image using legacy method (Jelly Bean, Kitkat)");
|
||||
if (imageUploadFilePathCallbackOld == null || resultCode != Activity.RESULT_OK) {
|
||||
AppLog.e(this, "Callback is null: " + (imageUploadFilePathCallbackOld == null)
|
||||
+ " resultCode: " + resultCode);
|
||||
if(imageUploadFilePathCallbackOld != null)
|
||||
imageUploadFilePathCallbackOld.onReceiveValue(null);
|
||||
return;
|
||||
}
|
||||
Uri results = null;
|
||||
if (data == null) {
|
||||
if (mCameraPhotoPath != null) {
|
||||
AppLog.v(this, "Intent has no data. Try to parse cameraPhotoPath");
|
||||
results = Uri.parse(mCameraPhotoPath);
|
||||
} else {
|
||||
AppLog.w(this, "Intent has no data and cameraPhotoPath is null");
|
||||
}
|
||||
} else {
|
||||
String dataString = data.getDataString();
|
||||
if (dataString != null) {
|
||||
AppLog.v(this, "Intent has data. Try to parse dataString");
|
||||
results = Uri.parse(dataString);
|
||||
} else {
|
||||
AppLog.w(this, "dataString is null");
|
||||
}
|
||||
}
|
||||
AppLog.v(this, "handle received result over to callback");
|
||||
imageUploadFilePathCallbackOld.onReceiveValue(results);
|
||||
imageUploadFilePathCallbackOld = null;
|
||||
return;
|
||||
}
|
||||
}
|
||||
AppLog.d(this, "onActivityResult(): "+requestCode);
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
}
|
||||
|
||||
|
@ -508,10 +412,13 @@ public class MainActivity extends AppCompatActivity
|
|||
}
|
||||
CustomFragment top = (CustomFragment) getTopFragment();
|
||||
if(top != null) {
|
||||
AppLog.d(this, "Top Fragment is not null");
|
||||
if(!top.onBackPressed()) {
|
||||
AppLog.d(this, "Top Fragment.onBackPressed was false");
|
||||
//TODO: Go back in Fragment backstack
|
||||
return;
|
||||
} else {
|
||||
AppLog.d(this, "Top Fragment.onBackPressed was true");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -737,13 +644,11 @@ public class MainActivity extends AppCompatActivity
|
|||
if (sharedSubject != null) {
|
||||
AppLog.v(this, "Append subject to shared text");
|
||||
String escapedSubject = WebHelper.escapeHtmlText(WebHelper.replaceUrlWithMarkdown(sharedSubject));
|
||||
//textToBeShared = "**" + escapedSubject + "** " + escapedBody;
|
||||
getStreamFragment().setTextToBeShared("**" + escapedSubject + "** " + escapedBody);
|
||||
} else {
|
||||
AppLog.v(this, "Set shared text; Subject: \"" + sharedSubject + "\" Body: \"" + sharedBody + "\"");
|
||||
//textToBeShared = escapedBody;
|
||||
getStreamFragment().setTextToBeShared(escapedBody);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//TODO: Implement?
|
||||
|
@ -759,7 +664,6 @@ public class MainActivity extends AppCompatActivity
|
|||
Toast.makeText(this, "Not yet implemented.", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
// TODO: Move from Javascript interface
|
||||
@Override
|
||||
public void onNotificationCountChanged(int notificationCount) {
|
||||
AppLog.i(this, "onNotificationCountChanged()");
|
||||
|
@ -767,7 +671,6 @@ public class MainActivity extends AppCompatActivity
|
|||
invalidateOptionsMenu();
|
||||
}
|
||||
|
||||
// TODO: Move from Javascript interface
|
||||
@Override
|
||||
public void onUnreadMessageCountChanged(int unreadMessageCount) {
|
||||
AppLog.i(this, "onUnreadMessageCountChanged()");
|
||||
|
@ -775,6 +678,19 @@ public class MainActivity extends AppCompatActivity
|
|||
invalidateOptionsMenu();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCustomTabsConnected() {
|
||||
if(customTabsSession == null) {
|
||||
AppLog.i(this, "CustomTabs warmup: "+customTabActivityHelper.warmup(0));
|
||||
customTabsSession = customTabActivityHelper.getSession();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCustomTabsDisconnected() {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("StatementWithEmptyBody")
|
||||
@Override
|
||||
public boolean onNavigationItemSelected(MenuItem item) {
|
||||
|
|
|
@ -1,23 +1,44 @@
|
|||
package com.github.dfa.diaspora_android.fragment;
|
||||
|
||||
import android.Manifest;
|
||||
import android.animation.ObjectAnimator;
|
||||
import android.app.Activity;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
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.util.AppLog;
|
||||
import com.github.dfa.diaspora_android.util.DiasporaUrlHelper;
|
||||
import com.github.dfa.diaspora_android.util.Helpers;
|
||||
import com.github.dfa.diaspora_android.util.WebHelper;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
||||
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.
|
||||
|
@ -29,13 +50,20 @@ public class StreamFragment extends WebViewFragment {
|
|||
private DiasporaUrlHelper urls;
|
||||
private Bundle webViewState;
|
||||
|
||||
private ValueCallback<Uri[]> imageUploadFilePathCallbackNew;
|
||||
private ValueCallback<Uri> imageUploadFilePathCallbackOld;
|
||||
private String mCameraPhotoPath;
|
||||
protected String textToBeShared;
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
AppLog.d(this, "onCreateView()");
|
||||
return inflater.inflate(R.layout.stream__fragment, container, false);
|
||||
}
|
||||
|
||||
@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);
|
||||
|
@ -47,9 +75,121 @@ public class StreamFragment extends WebViewFragment {
|
|||
progressBar,
|
||||
appSettings);
|
||||
|
||||
// Setup WebView
|
||||
webView.addJavascriptInterface(new JavaScriptInterface(), "AndroidBridge");
|
||||
|
||||
if(webView.getUrl() == null) {
|
||||
loadUrl(urls.getPodUrl());
|
||||
}
|
||||
|
||||
//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);
|
||||
|
||||
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;
|
||||
}
|
||||
});
|
||||
|
||||
this.setRetainInstance(true);
|
||||
}
|
||||
|
||||
|
@ -68,6 +208,19 @@ public class StreamFragment extends WebViewFragment {
|
|||
inflater.inflate(R.menu.stream__menu_bottom, menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
AppLog.d(this, "onActivityResult(): "+requestCode);
|
||||
switch (requestCode) {
|
||||
case MainActivity.INPUT_FILE_REQUEST_CODE_NEW:
|
||||
case MainActivity.INPUT_FILE_REQUEST_CODE_OLD:
|
||||
AppLog.d(this, "INPUT_FILE_REQUEST_CODE: "+requestCode);
|
||||
onImageUploadResult(requestCode, resultCode, data);
|
||||
return;
|
||||
}
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
AppLog.d(this, "StreamFragment.onOptionsItemSelected()");
|
||||
|
@ -130,18 +283,99 @@ public class StreamFragment extends WebViewFragment {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onBackPressed() {
|
||||
if(webView.canGoBack()) {
|
||||
webView.goBack();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ContextMenuWebView getWebView() {
|
||||
AppLog.d(this, "getWebView: "+(this.webView != null));
|
||||
return this.webView;
|
||||
}
|
||||
|
||||
public void setTextToBeShared(String text) {
|
||||
this.textToBeShared = text;
|
||||
}
|
||||
|
||||
public void onImageUploadResult(int requestCode, int resultCode, Intent data) {
|
||||
AppLog.d(this, "onImageUploadResult");
|
||||
switch (requestCode) {
|
||||
case MainActivity.INPUT_FILE_REQUEST_CODE_NEW: {
|
||||
AppLog.v(this, "Upload image using recent method (Lollipop+)");
|
||||
if (imageUploadFilePathCallbackNew == null || resultCode != Activity.RESULT_OK) {
|
||||
AppLog.e(this, "Callback is null: " + (imageUploadFilePathCallbackNew == null)
|
||||
+ " resultCode: " + resultCode);
|
||||
if (imageUploadFilePathCallbackNew != null)
|
||||
imageUploadFilePathCallbackNew.onReceiveValue(new Uri[]{});
|
||||
return;
|
||||
}
|
||||
Uri[] results = null;
|
||||
if (data == null) {
|
||||
if (mCameraPhotoPath != null) {
|
||||
AppLog.v(this, "Intent data is null. Try to parse cameraPhotoPath");
|
||||
results = new Uri[]{Uri.parse(mCameraPhotoPath)};
|
||||
} else {
|
||||
AppLog.w(this, "Intent data is null and cameraPhotoPath is null");
|
||||
}
|
||||
} else {
|
||||
String dataString = data.getDataString();
|
||||
if (dataString != null) {
|
||||
AppLog.v(this, "Intent has data. Try to parse dataString");
|
||||
results = new Uri[]{Uri.parse(dataString)};
|
||||
} else {
|
||||
AppLog.w(this, "dataString is null");
|
||||
}
|
||||
}
|
||||
AppLog.v(this, "handle received result over to callback");
|
||||
imageUploadFilePathCallbackNew.onReceiveValue(results);
|
||||
imageUploadFilePathCallbackNew = null;
|
||||
return;
|
||||
}
|
||||
case MainActivity.INPUT_FILE_REQUEST_CODE_OLD: {
|
||||
AppLog.v(this, "Upload image using legacy method (Jelly Bean, Kitkat)");
|
||||
if (imageUploadFilePathCallbackOld == null || resultCode != Activity.RESULT_OK) {
|
||||
AppLog.e(this, "Callback is null: " + (imageUploadFilePathCallbackOld == null)
|
||||
+ " resultCode: " + resultCode);
|
||||
if (imageUploadFilePathCallbackOld != null)
|
||||
imageUploadFilePathCallbackOld.onReceiveValue(null);
|
||||
return;
|
||||
}
|
||||
Uri results = null;
|
||||
if (data == null) {
|
||||
if (mCameraPhotoPath != null) {
|
||||
AppLog.v(this, "Intent has no data. Try to parse cameraPhotoPath");
|
||||
results = Uri.parse(mCameraPhotoPath);
|
||||
} else {
|
||||
AppLog.w(this, "Intent has no data and cameraPhotoPath is null");
|
||||
}
|
||||
} else {
|
||||
String dataString = data.getDataString();
|
||||
if (dataString != null) {
|
||||
AppLog.v(this, "Intent has data. Try to parse dataString");
|
||||
results = Uri.parse(dataString);
|
||||
} else {
|
||||
AppLog.w(this, "dataString is null");
|
||||
}
|
||||
}
|
||||
AppLog.v(this, "handle received result over to callback");
|
||||
imageUploadFilePathCallbackOld.onReceiveValue(results);
|
||||
imageUploadFilePathCallbackOld = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class JavaScriptInterface {
|
||||
@JavascriptInterface
|
||||
public void setUserProfile(final String webMessage) throws JSONException {
|
||||
PodUserProfile pup = ((App)getActivity().getApplication()).getPodUserProfile();
|
||||
AppLog.v(this, "StreamFragment.JavaScriptInterface.setUserProfile()");
|
||||
if (pup.isRefreshNeeded()) {
|
||||
AppLog.v(this, "PodUserProfile needs refresh; Try to parse JSON");
|
||||
pup.parseJson(webMessage);
|
||||
} else {
|
||||
AppLog.v(this, "No PodUserProfile refresh needed");
|
||||
}
|
||||
}
|
||||
|
||||
@JavascriptInterface
|
||||
public void contentHasBeenShared() {
|
||||
textToBeShared = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,9 +15,6 @@ import android.os.StrictMode;
|
|||
import android.support.design.widget.Snackbar;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.view.View;
|
||||
import android.webkit.JavascriptInterface;
|
||||
import android.webkit.ValueCallback;
|
||||
import android.webkit.WebChromeClient;
|
||||
import android.webkit.WebSettings;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
|
@ -28,13 +25,9 @@ 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.AppSettings;
|
||||
import com.github.dfa.diaspora_android.data.PodUserProfile;
|
||||
import com.github.dfa.diaspora_android.ui.ContextMenuWebView;
|
||||
import com.github.dfa.diaspora_android.ui.CustomWebViewClient;
|
||||
import com.github.dfa.diaspora_android.util.AppLog;
|
||||
import com.github.dfa.diaspora_android.util.WebHelper;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
|
@ -61,8 +54,6 @@ public abstract class WebViewFragment extends CustomFragment {
|
|||
protected ProgressBar progressBar;
|
||||
protected AppSettings appSettings;
|
||||
|
||||
protected String textToBeShared;
|
||||
|
||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
}
|
||||
|
@ -100,9 +91,6 @@ public abstract class WebViewFragment extends CustomFragment {
|
|||
//webView.setParentActivity(this);
|
||||
webView.setOverScrollMode(WebView.OVER_SCROLL_ALWAYS);
|
||||
|
||||
// Setup WebView
|
||||
webView.addJavascriptInterface(new JavaScriptInterface(), "AndroidBridge");
|
||||
|
||||
//Set proxy
|
||||
if (appSettings.isProxyEnabled()) {
|
||||
if (!setProxy(appSettings.getProxyHost(), appSettings.getProxyPort())) {
|
||||
|
@ -118,118 +106,6 @@ public abstract class WebViewFragment extends CustomFragment {
|
|||
*/
|
||||
this.webViewClient = new CustomWebViewClient((App) getActivity().getApplication(), webView);
|
||||
webView.setWebViewClient(webViewClient);
|
||||
|
||||
/*
|
||||
* 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);
|
||||
|
||||
if (textToBeShared != null) {
|
||||
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 (MainActivity.imageUploadFilePathCallbackNew != null) imageUploadFilePathCallbackNew.onReceiveValue(null);
|
||||
imageUploadFilePathCallbackNew = filePathCallback;
|
||||
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
||||
if (takePictureIntent.resolveActivity(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(contentLayout, 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);
|
||||
Log.d(App.TAG,"startActivityForResult");
|
||||
startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE_NEW);
|
||||
return true;
|
||||
}
|
||||
*/
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -311,25 +187,6 @@ public abstract class WebViewFragment extends CustomFragment {
|
|||
System.exit(0);
|
||||
}
|
||||
|
||||
private class JavaScriptInterface {
|
||||
@JavascriptInterface
|
||||
public void setUserProfile(final String webMessage) throws JSONException {
|
||||
PodUserProfile pup = ((App)getActivity().getApplication()).getPodUserProfile();
|
||||
AppLog.i(this, "StreamFragment.JavaScriptInterface.setUserProfile()");
|
||||
if (pup.isRefreshNeeded()) {
|
||||
AppLog.v(this, "PodUserProfile needs refresh; Try to parse JSON");
|
||||
pup.parseJson(webMessage);
|
||||
} else {
|
||||
AppLog.v(this, "No PodUserProfile refresh needed");
|
||||
}
|
||||
}
|
||||
|
||||
@JavascriptInterface
|
||||
public void contentHasBeenShared() {
|
||||
textToBeShared = null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
protected boolean makeScreenshotOfWebView(boolean hasToShareScreenshot) {
|
||||
AppLog.i(this, "StreamFragment.makeScreenshotOfWebView()");
|
||||
|
@ -412,7 +269,17 @@ public abstract class WebViewFragment extends CustomFragment {
|
|||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onBackPressed() {
|
||||
if(webView.canGoBack()) {
|
||||
webView.goBack();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void loadUrl(String url) {
|
||||
AppLog.v(this, "loadUrl("+url+")");
|
||||
getWebView().loadUrlNew(url);
|
||||
}
|
||||
|
||||
|
@ -421,6 +288,7 @@ public abstract class WebViewFragment extends CustomFragment {
|
|||
}
|
||||
|
||||
public void reloadUrl() {
|
||||
AppLog.v(this, "reloadUrl()");
|
||||
getWebView().reload();
|
||||
}
|
||||
|
||||
|
|
|
@ -118,6 +118,10 @@ public class CustomTabActivityHelper {
|
|||
return session.mayLaunchUrl(uri, extras, otherLikelyBundles);
|
||||
}
|
||||
|
||||
public boolean warmup(int flags) {
|
||||
return mClient.warmup(flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* A Callback for when the service is connected or disconnected. Use those callbacks to
|
||||
* handle UI changes when the service is connected or disconnected
|
||||
|
|
Loading…
Reference in a new issue