Commit 6432e80a authored by luca020400's avatar luca020400

Jelly: Add support for multiple windows

Change-Id: Ic1d2b395fd4343c442243f1008f72b363270206d
parent 5a0b5ccc
......@@ -83,7 +83,9 @@ import org.lineageos.jelly.history.HistoryActivity;
import org.lineageos.jelly.suggestions.SuggestionsAdapter;
import org.lineageos.jelly.ui.SearchBarController;
import org.lineageos.jelly.ui.UrlBarController;
import org.lineageos.jelly.utils.IntentUtils;
import org.lineageos.jelly.utils.PrefsUtils;
import org.lineageos.jelly.utils.TabUtils;
import org.lineageos.jelly.utils.UiUtils;
import org.lineageos.jelly.webview.WebViewCompat;
import org.lineageos.jelly.webview.WebViewExt;
......@@ -98,23 +100,16 @@ public class MainActivity extends WebViewExtActivity implements
SearchBarController.OnCancelListener {
private static final String TAG = MainActivity.class.getSimpleName();
private static final String PROVIDER = "org.lineageos.jelly.fileprovider";
private static final String EXTRA_INCOGNITO = "extra_incognito";
private static final String EXTRA_DESKTOP_MODE = "extra_desktop_mode";
public static final String EXTRA_URL = "extra_url";
public static final String EXTRA_UI_MODE = "extra_ui_mode";
public static final String EVENT_CHANGE_UI_MODE = "intent_change_ui_mode";
private static final String STATE_KEY_THEME_COLOR = "theme_color";
private static final int STORAGE_PERM_REQ = 423;
private static final int LOCATION_PERM_REQ = 424;
public static final String ACTION_URL_RESOLVED = "org.lineageos.jelly.action.URL_RESOLVED";
private final BroadcastReceiver mUrlResolvedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Intent resolvedIntent = intent.getParcelableExtra(Intent.EXTRA_INTENT);
if (TextUtils.equals(getPackageName(), resolvedIntent.getPackage())) {
String url = intent.getStringExtra(EXTRA_URL);
String url = intent.getStringExtra(IntentUtils.EXTRA_URL);
mWebView.loadUrl(url);
} else {
startActivity(resolvedIntent);
......@@ -199,16 +194,16 @@ public class MainActivity extends WebViewExtActivity implements
Intent intent = getIntent();
String url = intent.getDataString();
mIncognito = intent.getBooleanExtra(EXTRA_INCOGNITO, false);
mIncognito = intent.getBooleanExtra(IntentUtils.EXTRA_INCOGNITO, false);
boolean desktopMode = false;
// Restore from previous instance
if (savedInstanceState != null) {
mIncognito = savedInstanceState.getBoolean(EXTRA_INCOGNITO, mIncognito);
mIncognito = savedInstanceState.getBoolean(IntentUtils.EXTRA_INCOGNITO, mIncognito);
if (url == null || url.isEmpty()) {
url = savedInstanceState.getString(EXTRA_URL, null);
url = savedInstanceState.getString(IntentUtils.EXTRA_URL, null);
}
desktopMode = savedInstanceState.getBoolean(EXTRA_DESKTOP_MODE, false);
desktopMode = savedInstanceState.getBoolean(IntentUtils.EXTRA_DESKTOP_MODE, false);
mThemeColor = savedInstanceState.getInt(STATE_KEY_THEME_COLOR, 0);
}
......@@ -262,7 +257,7 @@ public class MainActivity extends WebViewExtActivity implements
@Override
protected void onStart() {
super.onStart();
registerReceiver(mUrlResolvedReceiver, new IntentFilter(ACTION_URL_RESOLVED));
registerReceiver(mUrlResolvedReceiver, new IntentFilter(IntentUtils.EVENT_URL_RESOLVED));
}
@Override
......@@ -355,9 +350,9 @@ public class MainActivity extends WebViewExtActivity implements
super.onSaveInstanceState(outState);
// Preserve webView status
outState.putString(EXTRA_URL, mWebView.getUrl());
outState.putBoolean(EXTRA_INCOGNITO, mWebView.isIncognito());
outState.putBoolean(EXTRA_DESKTOP_MODE, mWebView.isDesktopMode());
outState.putString(IntentUtils.EXTRA_URL, mWebView.getUrl());
outState.putBoolean(IntentUtils.EXTRA_INCOGNITO, mWebView.isIncognito());
outState.putBoolean(IntentUtils.EXTRA_DESKTOP_MODE, mWebView.isDesktopMode());
outState.putInt(STATE_KEY_THEME_COLOR, mThemeColor);
}
......@@ -381,10 +376,10 @@ public class MainActivity extends WebViewExtActivity implements
popupMenu.setOnMenuItemClickListener(item -> {
switch (item.getItemId()) {
case R.id.menu_new:
openInNewTab(null, false);
TabUtils.openInNewTab(this, null, false);
break;
case R.id.menu_incognito:
openInNewTab(null, true);
TabUtils.openInNewTab(this, null, true);
break;
case R.id.menu_reload:
mWebView.reload();
......@@ -448,16 +443,6 @@ public class MainActivity extends WebViewExtActivity implements
mSearchActive = false;
}
private void openInNewTab(String url, boolean incognito) {
Intent intent = new Intent(this, MainActivity.class);
if (url != null && !url.isEmpty()) {
intent.setData(Uri.parse(url));
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
intent.putExtra(EXTRA_INCOGNITO, incognito);
startActivity(intent);
}
private void shareUrl(String url) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, url);
......@@ -547,7 +532,7 @@ public class MainActivity extends WebViewExtActivity implements
View downloadLayout = view.findViewById(R.id.sheet_download);
tabLayout.setOnClickListener(v -> {
openInNewTab(url, mIncognito);
TabUtils.openInNewTab(this, url, mIncognito);
sheet.dismiss();
});
shareLayout.setOnClickListener(v -> {
......@@ -757,7 +742,7 @@ public class MainActivity extends WebViewExtActivity implements
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(this);
if (!UiUtils.isTablet(this)) {
manager.registerReceiver(mUiModeChangeReceiver, new IntentFilter(EVENT_CHANGE_UI_MODE));
manager.registerReceiver(mUiModeChangeReceiver, new IntentFilter(IntentUtils.EVENT_CHANGE_UI_MODE));
}
}
......
......@@ -32,6 +32,7 @@ import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
import org.lineageos.jelly.utils.IntentUtils;
import org.lineageos.jelly.utils.PrefsUtils;
import org.lineageos.jelly.utils.UiUtils;
......@@ -76,8 +77,8 @@ public class SettingsActivity extends AppCompatActivity {
getPreferenceScreen().removePreference(reachMode);
} else {
reachMode.setOnPreferenceClickListener(preference -> {
Intent intent = new Intent(MainActivity.EVENT_CHANGE_UI_MODE);
intent.putExtra(MainActivity.EVENT_CHANGE_UI_MODE, reachMode.isChecked());
Intent intent = new Intent(IntentUtils.EVENT_CHANGE_UI_MODE);
intent.putExtra(IntentUtils.EVENT_CHANGE_UI_MODE, reachMode.isChecked());
LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
return true;
});
......
/*
* Copyright (C) 2018 The LineageOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.lineageos.jelly.utils;
public final class IntentUtils {
public static final String EXTRA_DESKTOP_MODE = "extra_desktop_mode";
public static final String EXTRA_INCOGNITO = "extra_incognito";
public static final String EXTRA_URL = "extra_url";
public static final String EXTRA_UI_MODE = "extra_ui_mode";
public static final String EVENT_CHANGE_UI_MODE = "intent_change_ui_mode";
public static final String EVENT_URL_RESOLVED = "intent_url_resolved";
}
/*
* Copyright (C) 2018 The LineageOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.lineageos.jelly.utils;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import org.lineageos.jelly.MainActivity;
public final class TabUtils {
public static void openInNewTab(Context context, String url, boolean incognito) {
Intent intent = new Intent(context, MainActivity.class);
if (url != null && !url.isEmpty()) {
intent.setData(Uri.parse(url));
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
intent.putExtra(IntentUtils.EXTRA_INCOGNITO, incognito);
context.startActivity(intent);
}
}
......@@ -18,6 +18,7 @@ package org.lineageos.jelly.webview;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Message;
import android.net.Uri;
import android.view.View;
import android.webkit.GeolocationPermissions;
......@@ -27,9 +28,11 @@ import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.Toast;
import org.lineageos.jelly.MainActivity;
import org.lineageos.jelly.R;
import org.lineageos.jelly.history.HistoryProvider;
import org.lineageos.jelly.ui.UrlBarController;
import org.lineageos.jelly.utils.TabUtils;
class ChromeClient extends WebChromeClientCompat {
private final WebViewExtActivity mActivity;
......@@ -106,4 +109,13 @@ class ChromeClient extends WebChromeClientCompat {
public void onHideCustomView() {
mActivity.onHideCustomView();
}
@Override
public boolean onCreateWindow(WebView view, boolean isDialog,
boolean isUserGesture, Message resultMsg) {
WebView.HitTestResult result = view.getHitTestResult();
String url = result.getExtra();
TabUtils.openInNewTab(mActivity, url, mIncognito);
return true;
}
}
......@@ -42,6 +42,7 @@ import org.lineageos.jelly.IntentFilterCompat;
import org.lineageos.jelly.MainActivity;
import org.lineageos.jelly.R;
import org.lineageos.jelly.ui.UrlBarController;
import org.lineageos.jelly.utils.IntentUtils;
import org.lineageos.jelly.utils.UrlUtils;
import java.net.URISyntaxException;
......@@ -199,9 +200,9 @@ class WebClient extends WebViewClient {
return TextUtils.equals(lastIntent.getPackage(), ourPackageName) ? null : lastIntent;
}
Intent changeIntent = new Intent(MainActivity.ACTION_URL_RESOLVED)
Intent changeIntent = new Intent(IntentUtils.EVENT_URL_RESOLVED)
.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY)
.putExtra(MainActivity.EXTRA_URL, url);
.putExtra(IntentUtils.EXTRA_URL, url);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, changeIntent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
......
......@@ -95,6 +95,7 @@ public class WebViewExt extends WebView {
getSettings().setJavaScriptEnabled(PrefsUtils.getJavascript(mActivity));
getSettings().setJavaScriptCanOpenWindowsAutomatically(PrefsUtils.getJavascript(mActivity));
getSettings().setGeolocationEnabled(PrefsUtils.getLocation(mActivity));
getSettings().setSupportMultipleWindows(true);
getSettings().setBuiltInZoomControls(true);
getSettings().setDisplayZoomControls(false);
getSettings().setAppCacheEnabled(!mIncognito);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment