PageRenderTime 58ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/acr/browser/lightning/LightningView.java

https://gitlab.com/jbwhips883/Web-browser
Java | 922 lines | 798 code | 97 blank | 27 comment | 132 complexity | c51a5f7f78336170a64e43d4e6a0ccfe MD5 | raw file
Possible License(s): MPL-2.0
  1. /*
  2. * Copyright 2014 A.C.R. Development
  3. */
  4. package acr.browser.lightning;
  5. import java.io.ByteArrayInputStream;
  6. import java.io.File;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. import java.net.URISyntaxException;
  10. import net.fast.web.browser.R;
  11. import android.annotation.SuppressLint;
  12. import android.app.Activity;
  13. import android.app.AlertDialog;
  14. import android.content.Context;
  15. import android.content.DialogInterface;
  16. import android.content.Intent;
  17. import android.content.SharedPreferences;
  18. import android.graphics.Bitmap;
  19. import android.graphics.BitmapFactory;
  20. import android.net.MailTo;
  21. import android.net.Uri;
  22. import android.net.http.SslError;
  23. import android.os.Message;
  24. import android.text.InputType;
  25. import android.text.TextUtils;
  26. import android.text.method.PasswordTransformationMethod;
  27. import android.util.Log;
  28. import android.view.GestureDetector;
  29. import android.view.MotionEvent;
  30. import android.view.View;
  31. import android.view.GestureDetector.SimpleOnGestureListener;
  32. import android.view.View.OnTouchListener;
  33. import android.webkit.GeolocationPermissions;
  34. import android.webkit.HttpAuthHandler;
  35. import android.webkit.SslErrorHandler;
  36. import android.webkit.ValueCallback;
  37. import android.webkit.WebChromeClient;
  38. import android.webkit.WebResourceResponse;
  39. import android.webkit.WebSettings;
  40. import android.webkit.WebSettings.LayoutAlgorithm;
  41. import android.webkit.WebSettings.PluginState;
  42. import android.webkit.WebView;
  43. import android.webkit.WebViewClient;
  44. import android.widget.EditText;
  45. import android.widget.LinearLayout;
  46. public class LightningView {
  47. private Title mTitle;
  48. private WebView mWebView;
  49. private BrowserController mBrowserController;
  50. private GestureDetector mGestureDetector;
  51. private Activity mActivity;
  52. private WebSettings mSettings;
  53. private static int API = android.os.Build.VERSION.SDK_INT;
  54. private static String mPackageName;
  55. private static String mHomepage;
  56. private static String mDefaultUserAgent;
  57. private static Bitmap mWebpageBitmap;
  58. private static SharedPreferences mPreferences;
  59. private static boolean mWideViewPort;
  60. private static AdBlock mAdBlock;
  61. public LightningView(Activity activity, String url) {
  62. mActivity = activity;
  63. mWebView = new WebView(activity);
  64. mTitle = new Title(activity);
  65. mAdBlock = new AdBlock(activity);
  66. mPackageName = activity.getPackageName();
  67. mWebpageBitmap = BitmapFactory.decodeResource(activity.getResources(),
  68. R.drawable.ic_webpage);
  69. try {
  70. mBrowserController = (BrowserController) activity;
  71. } catch (ClassCastException e) {
  72. throw new ClassCastException(activity.toString()
  73. + " must implement BrowserController");
  74. }
  75. mWebView.setDrawingCacheBackgroundColor(0x00000000);
  76. mWebView.setFocusableInTouchMode(true);
  77. mWebView.setFocusable(true);
  78. mWebView.setAnimationCacheEnabled(false);
  79. mWebView.setDrawingCacheEnabled(true);
  80. mWebView.setBackgroundColor(activity.getResources().getColor(
  81. android.R.color.white));
  82. if (API > 15) {
  83. mWebView.getRootView().setBackground(null);
  84. } else {
  85. mWebView.getRootView().setBackgroundDrawable(null);
  86. }
  87. mWebView.setWillNotCacheDrawing(false);
  88. mWebView.setAlwaysDrawnWithCacheEnabled(true);
  89. mWebView.setScrollbarFadingEnabled(true);
  90. mWebView.setSaveEnabled(true);
  91. mWebView.setWebChromeClient(new LightningChromeClient(activity));
  92. mWebView.setWebViewClient(new LightningWebClient(activity));
  93. mWebView.setDownloadListener(new LightningDownloadListener(activity));
  94. mGestureDetector = new GestureDetector(activity,
  95. new CustomGestureListener());
  96. mWebView.setOnTouchListener(new OnTouchListener() {
  97. float mLocation = 0;
  98. float mY = 0;
  99. int mAction = 0;
  100. @Override
  101. public boolean onTouch(View view, MotionEvent arg1) {
  102. if (view != null && !view.hasFocus()) {
  103. view.requestFocus();
  104. }
  105. mAction = arg1.getAction();
  106. mY = arg1.getY();
  107. if (mAction == MotionEvent.ACTION_DOWN) {
  108. mLocation = mY;
  109. } else if (mAction == MotionEvent.ACTION_UP) {
  110. if ((mY - mLocation) > 10) {
  111. mBrowserController.showActionBar();
  112. } else if ((mY - mLocation) < -10) {
  113. mBrowserController.hideActionBar();
  114. }
  115. mLocation = 0;
  116. }
  117. mGestureDetector.onTouchEvent(arg1);
  118. return false;
  119. }
  120. });
  121. mDefaultUserAgent = mWebView.getSettings().getUserAgentString();
  122. mSettings = mWebView.getSettings();
  123. initializeSettings(mWebView.getSettings(), activity);
  124. initializePreferences(activity);
  125. if (url != null) {
  126. if (!url.equals("")) {
  127. mWebView.loadUrl(url);
  128. }
  129. } else {
  130. if (mHomepage.startsWith("about:home")) {
  131. mSettings.setUseWideViewPort(false);
  132. mWebView.loadUrl(getHomepage());
  133. } else if (mHomepage.startsWith("about:bookmarks")) {
  134. mBrowserController.openBookmarkPage(mWebView);
  135. } else {
  136. mWebView.loadUrl(mHomepage);
  137. }
  138. }
  139. }
  140. public String getHomepage() {
  141. String home = "";
  142. home = HomepageVariables.HEAD;
  143. switch (mPreferences.getInt(PreferenceConstants.SEARCH, 1)) {
  144. case 1:
  145. // GOOGLE_SEARCH;
  146. home = home + "file:///android_asset/google.png";
  147. // + "https://www.google.com/images/srpr/logo11w.png";
  148. home = home + HomepageVariables.MIDDLE;
  149. home = home + Constants.GOOGLE_SEARCH;
  150. break;
  151. case 2:
  152. // ANDROID SEARCH;
  153. home = home + "file:///android_asset/lightning.png";
  154. home = home + HomepageVariables.MIDDLE;
  155. home = home + Constants.ANDROID_SEARCH;
  156. break;
  157. case 3:
  158. // BING_SEARCH;
  159. home = home + "file:///android_asset/bing.png";
  160. // +
  161. // "http://upload.wikimedia.org/wikipedia/commons/thumb/b/b1/Bing_logo_%282013%29.svg/500px-Bing_logo_%282013%29.svg.png";
  162. home = home + HomepageVariables.MIDDLE;
  163. home = home + Constants.BING_SEARCH;
  164. break;
  165. case 4:
  166. // YAHOO_SEARCH;
  167. home = home + "file:///android_asset/yahoo.png";
  168. // +
  169. // "http://upload.wikimedia.org/wikipedia/commons/thumb/2/24/Yahoo%21_logo.svg/799px-Yahoo%21_logo.svg.png";
  170. home = home + HomepageVariables.MIDDLE;
  171. home = home + Constants.YAHOO_SEARCH;
  172. break;
  173. case 5:
  174. // STARTPAGE_SEARCH;
  175. home = home + "file:///android_asset/startpage.png";
  176. // + "https://startpage.com/graphics/startp_logo.gif";
  177. home = home + HomepageVariables.MIDDLE;
  178. home = home + Constants.STARTPAGE_SEARCH;
  179. break;
  180. case 6:
  181. // STARTPAGE_MOBILE
  182. home = home + "file:///android_asset/startpage.png";
  183. // + "https://startpage.com/graphics/startp_logo.gif";
  184. home = home + HomepageVariables.MIDDLE;
  185. home = home + Constants.STARTPAGE_MOBILE_SEARCH;
  186. case 7:
  187. // DUCK_SEARCH;
  188. home = home + "file:///android_asset/duckduckgo.png";
  189. // + "https://duckduckgo.com/assets/logo_homepage.normal.v101.png";
  190. home = home + HomepageVariables.MIDDLE;
  191. home = home + Constants.DUCK_SEARCH;
  192. break;
  193. case 8:
  194. // DUCK_LITE_SEARCH;
  195. home = home + "file:///android_asset/duckduckgo.png";
  196. // + "https://duckduckgo.com/assets/logo_homepage.normal.v101.png";
  197. home = home + HomepageVariables.MIDDLE;
  198. home = home + Constants.DUCK_LITE_SEARCH;
  199. break;
  200. case 9:
  201. // BAIDU_SEARCH;
  202. home = home + "file:///android_asset/baidu.png";
  203. // + "http://www.baidu.com/img/bdlogo.gif";
  204. home = home + HomepageVariables.MIDDLE;
  205. home = home + Constants.BAIDU_SEARCH;
  206. break;
  207. case 10:
  208. // YANDEX_SEARCH;
  209. home = home + "file:///android_asset/yandex.png";
  210. // +
  211. // "http://upload.wikimedia.org/wikipedia/commons/thumb/9/91/Yandex.svg/600px-Yandex.svg.png";
  212. home = home + HomepageVariables.MIDDLE;
  213. home = home + Constants.YANDEX_SEARCH;
  214. break;
  215. }
  216. home = home + HomepageVariables.END;
  217. File homepage = new File(mActivity.getCacheDir(), "homepage.html");
  218. try {
  219. FileWriter hWriter = new FileWriter(homepage, false);
  220. hWriter.write(home);
  221. hWriter.close();
  222. } catch (IOException e) {
  223. e.printStackTrace();
  224. }
  225. return Constants.FILE + homepage;
  226. }
  227. public synchronized void initializePreferences(Context context) {
  228. mPreferences = context.getSharedPreferences(PreferenceConstants.PREFERENCES, 0);
  229. mHomepage = mPreferences.getString(PreferenceConstants.HOMEPAGE, Constants.HOMEPAGE);
  230. mAdBlock.updatePreference();
  231. if (mSettings == null && mWebView != null) {
  232. mSettings = mWebView.getSettings();
  233. } else if (mSettings == null) {
  234. return;
  235. }
  236. mSettings.setGeolocationEnabled(mPreferences.getBoolean(PreferenceConstants.LOCATION,
  237. false));
  238. if (API < 19) {
  239. switch (mPreferences.getInt(PreferenceConstants.ADOBE_FLASH_SUPPORT, 0)) {
  240. case 0:
  241. mSettings.setPluginState(PluginState.OFF);
  242. break;
  243. case 1: {
  244. mSettings.setPluginState(PluginState.ON_DEMAND);
  245. break;
  246. }
  247. case 2: {
  248. mSettings.setPluginState(PluginState.ON);
  249. break;
  250. }
  251. default:
  252. break;
  253. }
  254. }
  255. switch (mPreferences.getInt(PreferenceConstants.USER_AGENT, 1)) {
  256. case 1:
  257. if (API > 16)
  258. mSettings.setUserAgentString(WebSettings
  259. .getDefaultUserAgent(context));
  260. else
  261. mSettings.setUserAgentString(mDefaultUserAgent);
  262. break;
  263. case 2:
  264. mSettings.setUserAgentString(Constants.DESKTOP_USER_AGENT);
  265. break;
  266. case 3:
  267. mSettings.setUserAgentString(Constants.MOBILE_USER_AGENT);
  268. break;
  269. case 4:
  270. mSettings.setUserAgentString(mPreferences.getString(
  271. PreferenceConstants.USER_AGENT_STRING, mDefaultUserAgent));
  272. break;
  273. }
  274. if (mPreferences.getBoolean(PreferenceConstants.SAVE_PASSWORDS, false)) {
  275. if (API < 18) {
  276. mSettings.setSavePassword(true);
  277. }
  278. mSettings.setSaveFormData(true);
  279. }
  280. if (mPreferences.getBoolean("java", true)) {
  281. mSettings.setJavaScriptEnabled(true);
  282. mSettings.setJavaScriptCanOpenWindowsAutomatically(true);
  283. }
  284. if (mPreferences.getBoolean("textreflow", false)) {
  285. mSettings.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);
  286. } else {
  287. mSettings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
  288. }
  289. mSettings.setBlockNetworkImage(mPreferences.getBoolean(PreferenceConstants.BLOCK_IMAGES,
  290. false));
  291. mSettings.setSupportMultipleWindows(mPreferences.getBoolean(
  292. PreferenceConstants.POPUPS, true));
  293. mSettings.setUseWideViewPort(mPreferences.getBoolean(PreferenceConstants.USE_WIDE_VIEWPORT,
  294. true));
  295. mWideViewPort = mPreferences.getBoolean(PreferenceConstants.USE_WIDE_VIEWPORT, true);
  296. mSettings.setLoadWithOverviewMode(mPreferences.getBoolean(
  297. PreferenceConstants.OVERVIEW_MODE, true));
  298. switch (mPreferences.getInt(PreferenceConstants.TEXT_SIZE, 3)) {
  299. case 1:
  300. mSettings.setTextZoom(200);
  301. break;
  302. case 2:
  303. mSettings.setTextZoom(150);
  304. break;
  305. case 3:
  306. mSettings.setTextZoom(100);
  307. break;
  308. case 4:
  309. mSettings.setTextZoom(75);
  310. break;
  311. case 5:
  312. mSettings.setTextZoom(50);
  313. break;
  314. }
  315. }
  316. @SuppressLint("SetJavaScriptEnabled")
  317. public void initializeSettings(WebSettings settings, Context context) {
  318. if (API < 18) {
  319. settings.setAppCacheMaxSize(Long.MAX_VALUE);
  320. }
  321. if (API < 17) {
  322. settings.setEnableSmoothTransition(true);
  323. }
  324. if (API > 16) {
  325. settings.setMediaPlaybackRequiresUserGesture(true);
  326. }
  327. if (API < 19) {
  328. settings.setDatabasePath(context.getFilesDir().getAbsolutePath()
  329. + "/databases");
  330. }
  331. settings.setDomStorageEnabled(true);
  332. settings.setAppCachePath(context.getCacheDir().toString());
  333. settings.setAppCacheEnabled(true);
  334. settings.setCacheMode(WebSettings.LOAD_DEFAULT);
  335. settings.setGeolocationDatabasePath(context.getCacheDir()
  336. .getAbsolutePath());
  337. settings.setAllowFileAccess(true);
  338. settings.setDatabaseEnabled(true);
  339. settings.setSupportZoom(true);
  340. settings.setBuiltInZoomControls(true);
  341. settings.setDisplayZoomControls(false);
  342. settings.setAllowContentAccess(true);
  343. settings.setDefaultTextEncodingName("utf-8");
  344. if (API > 16) {
  345. settings.setAllowFileAccessFromFileURLs(false);
  346. settings.setAllowUniversalAccessFromFileURLs(false);
  347. }
  348. }
  349. public boolean isShown() {
  350. if (mWebView != null)
  351. return mWebView.isShown();
  352. else
  353. return false;
  354. }
  355. public synchronized void onPause() {
  356. if (mWebView != null)
  357. mWebView.onPause();
  358. }
  359. public synchronized void onResume() {
  360. if (mWebView != null)
  361. mWebView.onResume();
  362. }
  363. public int getProgress() {
  364. if (mWebView != null) {
  365. return mWebView.getProgress();
  366. } else {
  367. return 100;
  368. }
  369. }
  370. public synchronized void stopLoading() {
  371. if (mWebView != null) {
  372. mWebView.stopLoading();
  373. }
  374. }
  375. public synchronized void pauseTimers() {
  376. if (mWebView != null) {
  377. mWebView.pauseTimers();
  378. }
  379. }
  380. public synchronized void resumeTimers() {
  381. if (mWebView != null) {
  382. mWebView.resumeTimers();
  383. }
  384. }
  385. public void requestFocus() {
  386. if (mWebView != null) {
  387. if (!mWebView.hasFocus())
  388. mWebView.requestFocus();
  389. }
  390. }
  391. public void setVisibility(int visible) {
  392. if (mWebView != null) {
  393. mWebView.setVisibility(visible);
  394. }
  395. }
  396. public void clearCache(boolean disk) {
  397. if (mWebView != null) {
  398. mWebView.clearCache(disk);
  399. }
  400. }
  401. public synchronized void reload() {
  402. if (mWebView != null) {
  403. mWebView.reload();
  404. }
  405. }
  406. public synchronized void find(String text) {
  407. if (mWebView != null) {
  408. if (API > 16) {
  409. mWebView.findAllAsync(text);
  410. } else {
  411. mWebView.findAll(text);
  412. }
  413. }
  414. }
  415. public synchronized void onDestroy() {
  416. if (mWebView != null) {
  417. mWebView.stopLoading();
  418. mWebView.onPause();
  419. mWebView.clearHistory();
  420. mWebView.setVisibility(View.GONE);
  421. mWebView.removeAllViews();
  422. mWebView.destroyDrawingCache();
  423. // mWebView.destroy(); //this is causing the segfault
  424. mWebView = null;
  425. }
  426. }
  427. public synchronized void goBack() {
  428. if (mWebView != null)
  429. mWebView.goBack();
  430. }
  431. public String getUserAgent() {
  432. if (mWebView != null) {
  433. return mWebView.getSettings().getUserAgentString();
  434. } else {
  435. return "";
  436. }
  437. }
  438. public synchronized void goForward() {
  439. if (mWebView != null)
  440. mWebView.goForward();
  441. }
  442. public boolean canGoBack() {
  443. if (mWebView != null) {
  444. return mWebView.canGoBack();
  445. } else {
  446. return false;
  447. }
  448. }
  449. public boolean canGoForward() {
  450. if (mWebView != null) {
  451. return mWebView.canGoForward();
  452. } else {
  453. return false;
  454. }
  455. }
  456. public WebView getWebView() {
  457. return mWebView;
  458. }
  459. public Bitmap getFavicon() {
  460. return mTitle.getFavicon();
  461. }
  462. public synchronized void loadUrl(String url) {
  463. if (mWebView != null)
  464. mWebView.loadUrl(url);
  465. }
  466. public synchronized void invalidate() {
  467. if (mWebView != null)
  468. mWebView.invalidate();
  469. }
  470. public String getTitle() {
  471. return mTitle.getTitle();
  472. }
  473. public String getUrl() {
  474. if (mWebView != null)
  475. return mWebView.getUrl();
  476. else
  477. return "";
  478. }
  479. public class LightningWebClient extends WebViewClient {
  480. Context mActivity;
  481. LightningWebClient(Context context) {
  482. mActivity = context;
  483. }
  484. @Override
  485. public WebResourceResponse shouldInterceptRequest(WebView view,
  486. String url) {
  487. if (mAdBlock.isAd(url)) {
  488. Log.i("Blocked Domain:", url);
  489. ByteArrayInputStream EMPTY = new ByteArrayInputStream(
  490. "".getBytes());
  491. WebResourceResponse response = new WebResourceResponse(
  492. "text/plain", "utf-8", EMPTY);
  493. return response;
  494. }
  495. return super.shouldInterceptRequest(view, url);
  496. }
  497. @Override
  498. public void onPageFinished(WebView view, String url) {
  499. if (view.isShown()) {
  500. view.invalidate();
  501. }
  502. mTitle.setTitle(view.getTitle());
  503. mBrowserController.update();
  504. }
  505. @Override
  506. public void onPageStarted(WebView view, String url, Bitmap favicon) {
  507. if (!mSettings.getUseWideViewPort()) {
  508. mSettings.setUseWideViewPort(mWideViewPort);
  509. }
  510. if (isShown()) {
  511. mBrowserController.updateUrl(url);
  512. mBrowserController.showActionBar();
  513. }
  514. mTitle.setFavicon(mWebpageBitmap);
  515. mBrowserController.update();
  516. }
  517. @Override
  518. public void onReceivedHttpAuthRequest(final WebView view,
  519. final HttpAuthHandler handler, final String host,
  520. final String realm) {
  521. AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
  522. final EditText name = new EditText(mActivity);
  523. final EditText password = new EditText(mActivity);
  524. LinearLayout passLayout = new LinearLayout(mActivity);
  525. passLayout.setOrientation(LinearLayout.VERTICAL);
  526. passLayout.addView(name);
  527. passLayout.addView(password);
  528. name.setHint(mActivity.getString(R.string.hint_username));
  529. password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
  530. password.setTransformationMethod(new PasswordTransformationMethod());
  531. password.setHint(mActivity.getString(R.string.hint_password));
  532. builder.setTitle(mActivity.getString(R.string.title_sign_in));
  533. builder.setView(passLayout);
  534. builder.setCancelable(true)
  535. .setPositiveButton(mActivity.getString(R.string.title_sign_in),
  536. new DialogInterface.OnClickListener() {
  537. @Override
  538. public void onClick(DialogInterface dialog,
  539. int id) {
  540. String user = name.getText().toString();
  541. String pass = password.getText().toString();
  542. handler.proceed(user.trim(), pass.trim());
  543. Log.i("Lightning", "Request Login");
  544. }
  545. })
  546. .setNegativeButton(mActivity.getString(R.string.action_cancel),
  547. new DialogInterface.OnClickListener() {
  548. @Override
  549. public void onClick(DialogInterface dialog,
  550. int id) {
  551. handler.cancel();
  552. }
  553. });
  554. AlertDialog alert = builder.create();
  555. alert.show();
  556. }
  557. @Override
  558. public void onScaleChanged(WebView view, float oldScale, float newScale) {
  559. if (view.isShown()) {
  560. view.invalidate();
  561. }
  562. }
  563. @Override
  564. public void onReceivedSslError(WebView view,
  565. final SslErrorHandler handler, SslError error) {
  566. AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
  567. builder.setTitle(mActivity.getString(R.string.title_warning));
  568. builder.setMessage(
  569. mActivity.getString(R.string.message_untrusted_certificate))
  570. .setCancelable(true)
  571. .setPositiveButton(mActivity.getString(R.string.action_yes),
  572. new DialogInterface.OnClickListener() {
  573. @Override
  574. public void onClick(DialogInterface dialog,
  575. int id) {
  576. handler.proceed();
  577. }
  578. })
  579. .setNegativeButton(mActivity.getString(R.string.action_no),
  580. new DialogInterface.OnClickListener() {
  581. @Override
  582. public void onClick(DialogInterface dialog,
  583. int id) {
  584. handler.cancel();
  585. }
  586. });
  587. AlertDialog alert = builder.create();
  588. if (error.getPrimaryError() == SslError.SSL_UNTRUSTED) {
  589. alert.show();
  590. } else {
  591. handler.proceed();
  592. }
  593. }
  594. @Override
  595. public void onFormResubmission(WebView view, final Message dontResend,
  596. final Message resend) {
  597. AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
  598. builder.setTitle(mActivity.getString(R.string.title_form_resubmission));
  599. builder.setMessage(mActivity.getString(R.string.message_form_resubmission))
  600. .setCancelable(true)
  601. .setPositiveButton(mActivity.getString(R.string.action_yes),
  602. new DialogInterface.OnClickListener() {
  603. @Override
  604. public void onClick(DialogInterface dialog,
  605. int id) {
  606. resend.sendToTarget();
  607. }
  608. })
  609. .setNegativeButton("No",
  610. new DialogInterface.OnClickListener() {
  611. @Override
  612. public void onClick(DialogInterface dialog,
  613. int id) {
  614. dontResend.sendToTarget();
  615. }
  616. });
  617. AlertDialog alert = builder.create();
  618. alert.show();
  619. }
  620. @Override
  621. public boolean shouldOverrideUrlLoading(WebView view, String url) {
  622. if (url.startsWith("market://")
  623. || url.startsWith("http://play.google.com/store/apps")
  624. || url.startsWith("https://play.google.com/store/apps")) {
  625. Intent urlIntent = new Intent(Intent.ACTION_VIEW,
  626. Uri.parse(url));
  627. urlIntent.putExtra(mPackageName + ".Origin", 1);
  628. mActivity.startActivity(urlIntent);
  629. return true;
  630. } else if (url.startsWith("http://www.youtube.com")
  631. || url.startsWith("https://www.youtube.com")
  632. || url.startsWith("http://m.youtube.com")
  633. || url.startsWith("https://m.youtube.com")) {
  634. Intent urlIntent = new Intent(Intent.ACTION_VIEW,
  635. Uri.parse(url));
  636. urlIntent.putExtra(mPackageName + ".Origin", 1);
  637. mActivity.startActivity(urlIntent);
  638. return true;
  639. } else if (url.startsWith("http://maps.google.com")
  640. || url.startsWith("https://maps.google.com")) {
  641. Intent urlIntent = new Intent(Intent.ACTION_VIEW,
  642. Uri.parse(url));
  643. urlIntent.putExtra(mPackageName + ".Origin", 1);
  644. mActivity.startActivity(urlIntent);
  645. return true;
  646. } else if (url.contains("tel:") || TextUtils.isDigitsOnly(url)) {
  647. mActivity.startActivity(new Intent(Intent.ACTION_DIAL, Uri
  648. .parse(url)));
  649. return true;
  650. } else if (url.contains("mailto:")) {
  651. MailTo mailTo = MailTo.parse(url);
  652. Intent i = Utils.newEmailIntent(mActivity, mailTo.getTo(),
  653. mailTo.getSubject(), mailTo.getBody(), mailTo.getCc());
  654. mActivity.startActivity(i);
  655. view.reload();
  656. return true;
  657. } else if (url.startsWith("magnet:?")) {
  658. Intent urlIntent = new Intent(Intent.ACTION_VIEW,
  659. Uri.parse(url));
  660. urlIntent.putExtra(mPackageName + ".Origin", 1);
  661. mActivity.startActivity(urlIntent);
  662. } else if (url.startsWith("intent://")) {
  663. Intent intent = null;
  664. try {
  665. intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
  666. } catch (URISyntaxException ex) {
  667. return false;
  668. }
  669. if (intent != null) {
  670. mActivity.startActivity(intent);
  671. return true;
  672. }
  673. }
  674. return super.shouldOverrideUrlLoading(view, url);
  675. }
  676. }
  677. public class LightningChromeClient extends WebChromeClient {
  678. Context mActivity;
  679. LightningChromeClient(Context context) {
  680. mActivity = context;
  681. }
  682. @Override
  683. public void onProgressChanged(WebView view, int newProgress) {
  684. if (isShown()) {
  685. mBrowserController.updateProgress(newProgress);
  686. }
  687. }
  688. @Override
  689. public void onReceivedIcon(WebView view, Bitmap icon) {
  690. mTitle.setFavicon(icon);
  691. mBrowserController.update();
  692. }
  693. @Override
  694. public void onReceivedTitle(WebView view, String title) {
  695. mTitle.setTitle(title);
  696. mBrowserController.update();
  697. mBrowserController.updateHistory(title, view.getUrl());
  698. }
  699. @Override
  700. public void onGeolocationPermissionsShowPrompt(final String origin,
  701. final GeolocationPermissions.Callback callback) {
  702. final boolean remember = true;
  703. AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
  704. builder.setTitle(mActivity.getString(R.string.location));
  705. String org = null;
  706. if (origin.length() > 50) {
  707. org = (String) origin.subSequence(0, 50) + "...";
  708. } else {
  709. org = origin;
  710. }
  711. builder.setMessage(org + mActivity.getString(R.string.message_location))
  712. .setCancelable(true)
  713. .setPositiveButton(mActivity.getString(R.string.action_allow),
  714. new DialogInterface.OnClickListener() {
  715. @Override
  716. public void onClick(DialogInterface dialog,
  717. int id) {
  718. callback.invoke(origin, true, remember);
  719. }
  720. })
  721. .setNegativeButton(mActivity.getString(R.string.action_dont_allow),
  722. new DialogInterface.OnClickListener() {
  723. @Override
  724. public void onClick(DialogInterface dialog,
  725. int id) {
  726. callback.invoke(origin, false, remember);
  727. }
  728. });
  729. AlertDialog alert = builder.create();
  730. alert.show();
  731. }
  732. @Override
  733. public boolean onCreateWindow(WebView view, boolean isDialog,
  734. boolean isUserGesture, Message resultMsg) {
  735. mBrowserController.onCreateWindow(isUserGesture, resultMsg);
  736. return isUserGesture;
  737. }
  738. @Override
  739. public void onCloseWindow(WebView window) {
  740. // TODO Auto-generated method stub
  741. super.onCloseWindow(window);
  742. }
  743. public void openFileChooser(ValueCallback<Uri> uploadMsg) {
  744. mBrowserController.openFileChooser(uploadMsg);
  745. }
  746. public void openFileChooser(ValueCallback<Uri> uploadMsg,
  747. String acceptType) {
  748. mBrowserController.openFileChooser(uploadMsg);
  749. }
  750. public void openFileChooser(ValueCallback<Uri> uploadMsg,
  751. String acceptType, String capture) {
  752. mBrowserController.openFileChooser(uploadMsg);
  753. }
  754. @Override
  755. public Bitmap getDefaultVideoPoster() {
  756. return mBrowserController.getDefaultVideoPoster();
  757. }
  758. @Override
  759. public View getVideoLoadingProgressView() {
  760. return mBrowserController.getVideoLoadingProgressView();
  761. }
  762. @Override
  763. public void onHideCustomView() {
  764. mBrowserController.onHideCustomView();
  765. super.onHideCustomView();
  766. }
  767. @Override
  768. public void onShowCustomView(View view, CustomViewCallback callback) {
  769. Activity activity = mBrowserController.getActivity();
  770. mBrowserController.onShowCustomView(view,
  771. activity.getRequestedOrientation(), callback);
  772. super.onShowCustomView(view, callback);
  773. }
  774. @Override
  775. @Deprecated
  776. public void onShowCustomView(View view, int requestedOrientation,
  777. CustomViewCallback callback) {
  778. mBrowserController.onShowCustomView(view, requestedOrientation,
  779. callback);
  780. super.onShowCustomView(view, requestedOrientation, callback);
  781. }
  782. }
  783. public class Title {
  784. private Bitmap mFavicon;
  785. private String mTitle;
  786. private Bitmap mDefaultIcon;
  787. public Title(Context context) {
  788. mDefaultIcon = BitmapFactory.decodeResource(context.getResources(),
  789. R.drawable.ic_webpage);
  790. mFavicon = mDefaultIcon;
  791. mTitle = mActivity.getString(R.string.action_new_tab);
  792. }
  793. public void setFavicon(Bitmap favicon) {
  794. mFavicon = favicon;
  795. if (mFavicon == null) {
  796. mFavicon = mDefaultIcon;
  797. }
  798. }
  799. public void setTitle(String title) {
  800. if (title == null) {
  801. title = "";
  802. }
  803. mTitle = title;
  804. }
  805. public void setTitleAndFavicon(String title, Bitmap favicon) {
  806. mTitle = title;
  807. mFavicon = favicon;
  808. if (mFavicon == null) {
  809. mFavicon = mDefaultIcon;
  810. }
  811. }
  812. public String getTitle() {
  813. return mTitle;
  814. }
  815. public Bitmap getFavicon() {
  816. return mFavicon;
  817. }
  818. }
  819. private class CustomGestureListener extends SimpleOnGestureListener {
  820. @Override
  821. public void onLongPress(MotionEvent e) {
  822. mBrowserController.onLongPress();
  823. }
  824. }
  825. }