Fix issue #4 - The app should not handle external links

This commit is contained in:
Thomas 2020-11-25 09:23:28 +01:00
parent ef44cd6f64
commit 06a0debbc0
8 changed files with 77 additions and 29 deletions

View File

@ -27,6 +27,7 @@ import androidx.appcompat.widget.SearchView;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
@ -37,9 +38,6 @@ import app.fedilab.mobilizon.drawer.InstanceAdapter;
import app.fedilab.mobilizon.viewmodel.SearchInstancesVM;
public class InstancePickerActivity extends AppCompatActivity {
@ -50,6 +48,7 @@ public class InstancePickerActivity extends AppCompatActivity {
private Search search;
private InstanceAdapter instanceAdapter;
private SearchInstancesVM searchInstancesVM;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -113,9 +112,9 @@ public class InstancePickerActivity extends AppCompatActivity {
@Override
public boolean onQueryTextSubmit(String query) {
max_id = "0";
if( query != null && query.trim().length() > 0) {
if (query != null && query.trim().length() > 0) {
search.setSearch(query.trim());
}else{
} else {
search.setSearch(null);
}
LinearLayoutManager mLayoutManager = new LinearLayoutManager(InstancePickerActivity.this);

View File

@ -56,12 +56,12 @@ import es.dmoral.toasty.Toasty;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
public static final int INPUT_FILE_REQUEST_CODE = 123;
private static final int PICK_INSTANCE = 546;
public static boolean isAuthenticated = false;
private RelativeLayout progress;
private WebView main_webview;
private FrameLayout webview_container;
private ValueCallback<Uri[]> mFilePathCallback;
private static final int PICK_INSTANCE = 546;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -97,7 +97,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
webview_container = findViewById(R.id.webview_container);
final ViewGroup videoLayout = findViewById(R.id.videoLayout);
String instance = Helper.getLiveInstance(MainActivity.this);
Helper.initializeWebview(MainActivity.this, main_webview, instance);
Helper.initializeWebview(MainActivity.this, main_webview);
MobilizonWebChromeClient mobilizonWebChromeClient = new MobilizonWebChromeClient(MainActivity.this, main_webview, webview_container, videoLayout);
main_webview.setWebChromeClient(mobilizonWebChromeClient);
main_webview.setWebViewClient(new MobilizonWebViewClient(MainActivity.this));
@ -114,6 +114,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
public void onShown(Snackbar snackbar) {
// Event handler for when the given Snackbar is visible
}
@Override
public void onDismissed(Snackbar snackbar, int event) {
// Event handler for when the given Snackbar has been dismissed
@ -134,8 +135,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
runOnUiThread(() -> {
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean forcedCacheDeletedDone = sharedpreferences.getBoolean(Helper.FORCED_CACHE_DELETE_DONE, false);
String previousVersion = sharedpreferences.getString(Helper.PREF_VERSION+instance, null);
if( !forcedCacheDeletedDone || (previousVersion != null && previousVersion.trim().compareTo(instanceNodeInfo.getSoftware().getVersion()) != 0)) {
String previousVersion = sharedpreferences.getString(Helper.PREF_VERSION + instance, null);
if (!forcedCacheDeletedDone || (previousVersion != null && previousVersion.trim().compareTo(instanceNodeInfo.getSoftware().getVersion()) != 0)) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_VERSION + instance.trim(), instanceNodeInfo.getSoftware().getVersion().trim());
editor.putBoolean(Helper.FORCED_CACHE_DELETE_DONE, true);
@ -143,16 +144,18 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
File dir = new File(getCacheDir().getPath());
try {
Helper.deleteDir(dir);
}catch (Exception ignored){}
} catch (Exception ignored) {
}
main_webview.reload();
}else if(previousVersion == null) {
} else if (previousVersion == null) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_VERSION + instance.trim(), instanceNodeInfo.getSoftware().getVersion().trim());
editor.apply();
}
});
}
} catch (Exception ignored) {}
} catch (Exception ignored) {
}
}).start();
}

View File

@ -13,6 +13,7 @@ package app.fedilab.mobilizon.client;
*
* You should have received a copy of the GNU General Public License along with Mobilizon app; if not,
* see <http://www.gnu.org/licenses>. */
import app.fedilab.mobilizon.client.entities.data.InstanceData;
import retrofit2.Call;
import retrofit2.http.GET;

View File

@ -37,7 +37,6 @@ import app.fedilab.mobilizon.client.entities.data.InstanceData.Instance;
import static android.app.Activity.RESULT_OK;
public class InstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final List<Instance> instances;
@ -73,8 +72,6 @@ public class InstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
holder.host.setText(instance.getHost());
StringBuilder languages = new StringBuilder();
if (instance.getLanguages() != null && instance.getLanguages().size() > 0) {
for (InstanceData.Language language : instance.getLanguages()) {

View File

@ -2,9 +2,14 @@ package app.fedilab.mobilizon.helper;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.net.Uri;
import android.os.Parcelable;
import android.util.Base64;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
@ -13,6 +18,10 @@ import android.webkit.WebView;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import app.fedilab.mobilizon.R;
/* Copyright 2020 Thomas Schneider
*
@ -56,7 +65,7 @@ public class Helper {
* @param webView WebView
*/
@SuppressLint("SetJavaScriptEnabled")
public static void initializeWebview(Context context, WebView webView, String instance) {
public static void initializeWebview(Context context, WebView webView) {
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setUseWideViewPort(true);
@ -87,6 +96,7 @@ public class Helper {
/**
* Delete a directory
*
* @param dir File
* @return boolean
*/
@ -100,7 +110,7 @@ public class Helper {
}
}
return dir.delete();
} else if(dir!= null && dir.isFile()) {
} else if (dir != null && dir.isFile()) {
return dir.delete();
} else {
return false;
@ -108,7 +118,6 @@ public class Helper {
}
@SuppressWarnings({"ResultOfMethodCallIgnored"})
public static void injectCSS(Activity activity, WebView view, String cssFile) {
try {
@ -136,4 +145,34 @@ public class Helper {
CookieManager.getInstance().flush();
}
/**
* Forward the intent (open an URL) to another app
*
* @param activity Activity
* @param url String
*/
public static void forwardToAnotherApp(Activity activity, String url) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "text/html");
@SuppressLint("QueryPermissionsNeeded") List<ResolveInfo> activities = activity.getPackageManager().queryIntentActivities(intent, 0);
ArrayList<Intent> targetIntents = new ArrayList<>();
String thisPackageName = activity.getPackageName();
for (ResolveInfo currentInfo : activities) {
String packageName = currentInfo.activityInfo.packageName;
if (!thisPackageName.equals(packageName)) {
Intent targetIntent = new Intent(android.content.Intent.ACTION_VIEW);
targetIntent.setDataAndType(intent.getData(), intent.getType());
targetIntent.setPackage(intent.getPackage());
targetIntent.setComponent(new ComponentName(packageName, currentInfo.activityInfo.name));
targetIntents.add(targetIntent);
}
}
if (targetIntents.size() > 0) {
Intent chooserIntent = Intent.createChooser(targetIntents.remove(0), activity.getString(R.string.open_with));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[]{}));
activity.startActivity(chooserIntent);
}
}
}

View File

@ -18,7 +18,6 @@ package app.fedilab.mobilizon.viewmodel;
import android.os.Handler;
import android.os.Looper;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

View File

@ -41,16 +41,14 @@ import static app.fedilab.mobilizon.MainActivity.INPUT_FILE_REQUEST_CODE;
public class MobilizonWebChromeClient extends WebChromeClient implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener {
private FrameLayout videoViewContainer;
private CustomViewCallback videoViewCallback;
private ToggledFullscreenCallback toggledFullscreenCallback;
private final WebView webView;
private final View activityNonVideoView;
private final ViewGroup activityVideoView;
private boolean isVideoFullscreen;
private final Activity activity;
private FrameLayout videoViewContainer;
private CustomViewCallback videoViewCallback;
private ToggledFullscreenCallback toggledFullscreenCallback;
private boolean isVideoFullscreen;
public MobilizonWebChromeClient(Activity activity, WebView webView, FrameLayout activityNonVideoView, ViewGroup activityVideoView) {
@ -108,7 +106,6 @@ public class MobilizonWebChromeClient extends WebChromeClient implements MediaPl
public void onShowCustomView(View view, CustomViewCallback callback) {
if (view instanceof FrameLayout) {
if (((AppCompatActivity) activity).getSupportActionBar() != null)
//noinspection ConstantConditions
((AppCompatActivity) activity).getSupportActionBar().hide();
// A video wants to be shown
FrameLayout frameLayout = (FrameLayout) view;
@ -173,7 +170,6 @@ public class MobilizonWebChromeClient extends WebChromeClient implements MediaPl
@Override
public void onHideCustomView() {
if (((AppCompatActivity) activity).getSupportActionBar() != null)
//noinspection ConstantConditions
((AppCompatActivity) activity).getSupportActionBar().show();
// This method should be manually called on video end in all cases because it's not always called automatically.
// This method must be manually called on back key press (from this class' onBackPressed() method).

View File

@ -33,6 +33,8 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout;
import com.google.android.material.snackbar.Snackbar;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
@ -52,8 +54,20 @@ public class MobilizonWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
try {
URL url_ = new URL(url);
String host = url_.getHost();
if (host.contains(Helper.getLiveInstance(activity))) {
view.loadUrl(url);
return true;
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
view.stopLoading();
Helper.forwardToAnotherApp(activity, url);
return false;
}