Advertisement
Guest User

Untitled

a guest
Jan 1st, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.65 KB | None | 0 0
  1. package com.myapp.com.webviewapp.fragment;
  2.  
  3. import android.app.Activity;
  4. import android.content.ActivityNotFoundException;
  5. import android.content.Intent;
  6. import android.net.MailTo;
  7. import android.net.Uri;
  8. import android.os.Bundle;
  9. import android.os.Handler;
  10. import android.support.v4.widget.SwipeRefreshLayout;
  11. import android.view.KeyEvent;
  12. import android.view.LayoutInflater;
  13. import android.view.Menu;
  14. import android.view.MenuInflater;
  15. import android.view.MenuItem;
  16. import android.view.MotionEvent;
  17. import android.view.View;
  18. import android.view.ViewGroup;
  19. import android.webkit.WebChromeClient;
  20. import android.webkit.WebSettings;
  21. import android.webkit.WebView;
  22. import android.webkit.WebViewClient;
  23. import android.widget.Toast;
  24.  
  25. import com.google.android.gms.ads.AdRequest;
  26. import com.google.android.gms.ads.AdView;
  27. import com.myapp.com.webviewapp.R;
  28. import com.myapp.com.webviewapp.WebViewAppConfig;
  29. import com.myapp.com.webviewapp.task.TaskFragment;
  30. import com.myapp.com.webviewapp.utility.DownloadUtility;
  31. import com.myapp.com.webviewapp.utility.Logcat;
  32. import com.myapp.com.webviewapp.utility.NetworkManager;
  33. import com.myapp.com.webviewapp.view.ViewState;
  34.  
  35.  
  36. public class MainFragment extends TaskFragment implements SwipeRefreshLayout.OnRefreshListener
  37. {
  38. private static final String ARGUMENT_URL = "url";
  39. private static final String ARGUMENT_SHARE = "share";
  40.  
  41. private boolean mActionBarProgress = false;
  42. private ViewState mViewState = null;
  43. private View mRootView;
  44. private String mUrl = "about:blank";
  45. private String mShare;
  46. private boolean mLocal = false;
  47.  
  48.  
  49. public static MainFragment newInstance(String url, String share)
  50. {
  51. MainFragment fragment = new MainFragment();
  52.  
  53. // arguments
  54. Bundle arguments = new Bundle();
  55. arguments.putString(ARGUMENT_URL, url);
  56. arguments.putString(ARGUMENT_SHARE, share);
  57. fragment.setArguments(arguments);
  58.  
  59. return fragment;
  60. }
  61.  
  62.  
  63. @Override
  64. public void onAttach(Activity activity)
  65. {
  66. super.onAttach(activity);
  67. }
  68.  
  69.  
  70. @Override
  71. public void onCreate(Bundle savedInstanceState)
  72. {
  73. super.onCreate(savedInstanceState);
  74.  
  75. setHasOptionsMenu(true);
  76. setRetainInstance(true);
  77.  
  78. // handle fragment arguments
  79. Bundle arguments = getArguments();
  80. if(arguments != null)
  81. {
  82. handleArguments(arguments);
  83. }
  84. }
  85.  
  86.  
  87. @Override
  88. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  89. {
  90. mRootView = inflater.inflate(R.layout.fragment_main, container, false);
  91. return mRootView;
  92. }
  93.  
  94.  
  95. @Override
  96. public void onActivityCreated(Bundle savedInstanceState)
  97. {
  98. super.onActivityCreated(savedInstanceState);
  99.  
  100. // restore webview state
  101. if(savedInstanceState!=null)
  102. {
  103. WebView webView = (WebView) mRootView.findViewById(R.id.fragment_main_webview);
  104. webView.restoreState(savedInstanceState);
  105. }
  106.  
  107. // setup webview
  108. renderView();
  109.  
  110. webView.getSettings().setUserAgentString("my-app");
  111.  
  112.  
  113. // pull to refresh
  114. SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) mRootView.findViewById(R.id.container_swipe_refresh);
  115. swipeRefreshLayout.setColorSchemeResources(R.color.global_bg_front_inverse, R.color.global_bg_front, R.color.global_bg_front_inverse, R.color.global_bg_front);
  116. swipeRefreshLayout.setOnRefreshListener(this);
  117.  
  118. // load and show data
  119. if(mViewState==null || mViewState==ViewState.OFFLINE)
  120.  
  121. {
  122. loadData();
  123. }
  124. else if(mViewState==ViewState.CONTENT)
  125. {
  126. showContent();
  127. }
  128. else if(mViewState==ViewState.PROGRESS)
  129. {
  130. showContent();
  131. }
  132. else if(mViewState==ViewState.EMPTY)
  133. {
  134. showEmpty();
  135. }
  136.  
  137. // progress in action bar
  138. showActionBarProgress(mActionBarProgress);
  139. }
  140.  
  141.  
  142. @Override
  143. public void onStart()
  144. {
  145. super.onStart();
  146. }
  147.  
  148.  
  149. @Override
  150. public void onResume()
  151. {
  152. super.onResume();
  153. }
  154.  
  155.  
  156. @Override
  157. public void onPause()
  158. {
  159. super.onPause();
  160. }
  161.  
  162.  
  163. @Override
  164. public void onStop()
  165. {
  166. super.onStop();
  167. }
  168.  
  169.  
  170. @Override
  171. public void onDestroyView()
  172. {
  173. super.onDestroyView();
  174. mRootView = null;
  175. }
  176.  
  177.  
  178. @Override
  179. public void onDestroy()
  180. {
  181. super.onDestroy();
  182. }
  183.  
  184.  
  185. @Override
  186. public void onDetach()
  187. {
  188. super.onDetach();
  189. }
  190.  
  191.  
  192. @Override
  193. public void onSaveInstanceState(Bundle outState)
  194. {
  195. // save current instance state
  196. super.onSaveInstanceState(outState);
  197. setUserVisibleHint(true);
  198.  
  199. // save webview state
  200. WebView webView = (WebView) mRootView.findViewById(R.id.fragment_main_webview);
  201. webView.saveState(outState);
  202. }
  203.  
  204.  
  205. @Override
  206. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
  207. {
  208. // action bar menu
  209. super.onCreateOptionsMenu(menu, inflater);
  210. inflater.inflate(R.menu.menu_main, menu);
  211.  
  212. // show or hide share button
  213. MenuItem share = menu.findItem(R.id.ab_button_share);
  214. share.setVisible(mShare!=null && !mShare.trim().equals(""));
  215. }
  216.  
  217.  
  218. @Override
  219. public boolean onOptionsItemSelected(MenuItem item)
  220. {
  221. // action bar menu behaviour
  222. switch(item.getItemId())
  223. {
  224. case R.id.ab_button_share:
  225. startShareActivity(getString(R.string.app_name), mShare);
  226. return true;
  227.  
  228. default:
  229. return super.onOptionsItemSelected(item);
  230. }
  231. }
  232.  
  233.  
  234. @Override
  235. public void onRefresh()
  236. {
  237. runTaskCallback(new Runnable()
  238. {
  239. @Override
  240. public void run()
  241. {
  242. refreshData();
  243. }
  244. });
  245. }
  246.  
  247.  
  248. private void handleArguments(Bundle arguments)
  249. {
  250. if(arguments.containsKey(ARGUMENT_URL))
  251. {
  252. mUrl = arguments.getString(ARGUMENT_URL);
  253. mLocal = mUrl.contains("file://");
  254. }
  255. if(arguments.containsKey(ARGUMENT_SHARE))
  256. {
  257. mShare = arguments.getString(ARGUMENT_SHARE);
  258. }
  259. }
  260.  
  261.  
  262. private void loadData()
  263. {
  264. if(NetworkManager.isOnline(getActivity()) || mLocal)
  265. {
  266. // show progress
  267. showProgress();
  268.  
  269. // load web url
  270. WebView webView = (WebView) mRootView.findViewById(R.id.fragment_main_webview);
  271. webView.loadUrl(mUrl);
  272. }
  273. else
  274. {
  275. showOffline();
  276. }
  277. }
  278.  
  279.  
  280. public void refreshData()
  281. {
  282. if(NetworkManager.isOnline(getActivity()) || mLocal)
  283. {
  284. // show progress in action bar
  285. showActionBarProgress(true);
  286.  
  287. // load web url
  288. WebView webView = (WebView) mRootView.findViewById(R.id.fragment_main_webview);
  289. webView.loadUrl(webView.getUrl());
  290. }
  291. else
  292. {
  293. showActionBarProgress(false);
  294. Toast.makeText(getActivity(), R.string.global_offline_toast, Toast.LENGTH_LONG).show();
  295. }
  296. }
  297.  
  298.  
  299. private void showActionBarProgress(boolean visible)
  300. {
  301. // show pull to refresh progress bar
  302. SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) mRootView.findViewById(R.id.container_swipe_refresh);
  303.  
  304. swipeRefreshLayout.setRefreshing(visible);
  305. swipeRefreshLayout.setEnabled(!visible);
  306.  
  307. mActionBarProgress = visible;
  308. }
  309.  
  310.  
  311. private void showContent()
  312. {
  313. // show content container
  314. ViewGroup containerContent = (ViewGroup) mRootView.findViewById(R.id.container_content);
  315. ViewGroup containerProgress = (ViewGroup) mRootView.findViewById(R.id.container_progress);
  316. ViewGroup containerOffline = (ViewGroup) mRootView.findViewById(R.id.container_offline);
  317. ViewGroup containerEmpty = (ViewGroup) mRootView.findViewById(R.id.container_empty);
  318. containerContent.setVisibility(View.VISIBLE);
  319. containerProgress.setVisibility(View.GONE);
  320. containerOffline.setVisibility(View.GONE);
  321. containerEmpty.setVisibility(View.GONE);
  322. mViewState = ViewState.CONTENT;
  323. }
  324.  
  325.  
  326. private void showContent(final long delay)
  327. {
  328. final Handler timerHandler = new Handler();
  329. final Runnable timerRunnable = new Runnable()
  330. {
  331. @Override
  332. public void run()
  333. {
  334. runTaskCallback(new Runnable()
  335. {
  336. public void run()
  337. {
  338. if(getActivity()!=null && mRootView!=null)
  339. {
  340. Logcat.d("Fragment.timerRunnable()");
  341. showContent();
  342. }
  343. }
  344. });
  345. }
  346. };
  347. timerHandler.postDelayed(timerRunnable, delay);
  348. }
  349.  
  350.  
  351. private void showProgress()
  352. {
  353. // show progress container
  354. ViewGroup containerContent = (ViewGroup) mRootView.findViewById(R.id.container_content);
  355. ViewGroup containerProgress = (ViewGroup) mRootView.findViewById(R.id.container_progress);
  356. ViewGroup containerOffline = (ViewGroup) mRootView.findViewById(R.id.container_offline);
  357. ViewGroup containerEmpty = (ViewGroup) mRootView.findViewById(R.id.container_empty);
  358. containerContent.setVisibility(View.GONE);
  359. containerProgress.setVisibility(View.VISIBLE);
  360. containerOffline.setVisibility(View.GONE);
  361. containerEmpty.setVisibility(View.GONE);
  362. mViewState = ViewState.PROGRESS;
  363. }
  364.  
  365.  
  366. private void showOffline()
  367. {
  368. // show offline container
  369. ViewGroup containerContent = (ViewGroup) mRootView.findViewById(R.id.container_content);
  370. ViewGroup containerProgress = (ViewGroup) mRootView.findViewById(R.id.container_progress);
  371. ViewGroup containerOffline = (ViewGroup) mRootView.findViewById(R.id.container_offline);
  372. ViewGroup containerEmpty = (ViewGroup) mRootView.findViewById(R.id.container_empty);
  373. containerContent.setVisibility(View.GONE);
  374. containerProgress.setVisibility(View.GONE);
  375. containerOffline.setVisibility(View.VISIBLE);
  376. containerEmpty.setVisibility(View.GONE);
  377. mViewState = ViewState.OFFLINE;
  378. }
  379.  
  380.  
  381. private void showEmpty()
  382. {
  383. // show empty container
  384. ViewGroup containerContent = (ViewGroup) mRootView.findViewById(R.id.container_content);
  385. ViewGroup containerProgress = (ViewGroup) mRootView.findViewById(R.id.container_progress);
  386. ViewGroup containerOffline = (ViewGroup) mRootView.findViewById(R.id.container_offline);
  387. ViewGroup containerEmpty = (ViewGroup) mRootView.findViewById(R.id.container_empty);
  388. containerContent.setVisibility(View.GONE);
  389. containerProgress.setVisibility(View.GONE);
  390. containerOffline.setVisibility(View.GONE);
  391. containerEmpty.setVisibility(View.VISIBLE);
  392. mViewState = ViewState.EMPTY;
  393. }
  394.  
  395.  
  396. private void renderView()
  397. {
  398. // reference
  399. final WebView webView = (WebView) mRootView.findViewById(R.id.fragment_main_webview);
  400. final AdView adView = (AdView) mRootView.findViewById(R.id.fragment_main_adview);
  401.  
  402. // webview settings
  403. webView.getSettings().setBuiltInZoomControls(false);
  404. webView.getSettings().setSupportZoom(true);
  405. webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
  406. webView.getSettings().setJavaScriptEnabled(true);
  407. webView.setBackgroundColor(getResources().getColor(R.color.global_bg_front));
  408. webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); // fixes scrollbar on Froyo
  409. webView.setWebChromeClient(new WebChromeClient()); // http://stackoverflow.com/questions/8541443/whats-causing-this-nullpointerexception
  410. webView.setWebViewClient(new WebViewClient()
  411. {
  412. @Override
  413. public void onPageFinished(final WebView view, final String url)
  414. {
  415. runTaskCallback(new Runnable()
  416. {
  417. public void run()
  418. {
  419. if(getActivity()!=null)
  420. {
  421. showContent(500); // hide progress bar with delay to show webview content smoothly
  422. showActionBarProgress(false);
  423. }
  424. }
  425. });
  426. }
  427.  
  428.  
  429. @Override
  430. public void onReceivedError(final WebView view, final int errorCode, final String description, final String failingUrl)
  431. {
  432. runTaskCallback(new Runnable()
  433. {
  434. public void run()
  435. {
  436. if(getActivity()!=null)
  437. {
  438. showEmpty();
  439. showActionBarProgress(false);
  440. }
  441. }
  442. });
  443. }
  444.  
  445.  
  446. @Override
  447. public boolean shouldOverrideUrlLoading(WebView view, String url)
  448. {
  449. if(DownloadUtility.isDownloadableFile(url))
  450. {
  451. Toast.makeText(getActivity(), R.string.fragment_main_downloading, Toast.LENGTH_LONG).show();
  452. DownloadUtility.downloadFile(getActivity(), url, DownloadUtility.getFileName(url));
  453. return true;
  454. }
  455. else if(url!=null && url.startsWith("https://play.google.com/store/"))
  456. {
  457. startWebActivity(url);
  458. return true;
  459. }
  460. else if(url!=null && (url.startsWith("http://") || url.startsWith("https://")))
  461. {
  462. if(WebViewAppConfig.OPEN_LINKS_IN_EXTERNAL_BROWSER)
  463. {
  464. startWebActivity(url);
  465. return true;
  466. }
  467. else
  468. {
  469. showActionBarProgress(true);
  470. return false;
  471. }
  472. }
  473. else if(url!=null && url.startsWith("mailto:"))
  474. {
  475. MailTo mailTo = MailTo.parse(url);
  476. startEmailActivity(mailTo.getTo(), mailTo.getSubject(), mailTo.getBody());
  477. return true;
  478. }
  479. else if(url!=null && url.startsWith("tel:"))
  480. {
  481. startCallActivity(url);
  482. return true;
  483. }
  484. else if(url!=null && url.startsWith("sms:"))
  485. {
  486. startSmsActivity(url);
  487. return true;
  488. }
  489. else if(url!=null && url.startsWith("geo:"))
  490. {
  491. startMapSearchActivity(url);
  492. return true;
  493. }
  494. else
  495. {
  496. return false;
  497. }
  498. }
  499. });
  500. webView.setOnKeyListener(new View.OnKeyListener()
  501. {
  502. @Override
  503. public boolean onKey(View v, int keyCode, KeyEvent event)
  504. {
  505. if(event.getAction() == KeyEvent.ACTION_DOWN)
  506. {
  507. WebView webView = (WebView) v;
  508.  
  509. switch(keyCode)
  510. {
  511. case KeyEvent.KEYCODE_BACK:
  512. if(webView.canGoBack())
  513. {
  514. webView.goBack();
  515. return true;
  516. }
  517. break;
  518. }
  519. }
  520.  
  521. return false;
  522. }
  523. });
  524. webView.requestFocus(View.FOCUS_DOWN); // http://android24hours.blogspot.cz/2011/12/android-soft-keyboard-not-showing-on.html
  525. webView.setOnTouchListener(new View.OnTouchListener()
  526. {
  527. @Override
  528. public boolean onTouch(View v, MotionEvent event)
  529. {
  530. switch(event.getAction())
  531. {
  532. case MotionEvent.ACTION_DOWN:
  533. case MotionEvent.ACTION_UP:
  534. if(!v.hasFocus())
  535. {
  536. v.requestFocus();
  537. }
  538. break;
  539. }
  540.  
  541. return false;
  542. }
  543. });
  544.  
  545. // admob
  546. if(WebViewAppConfig.ADMOB && NetworkManager.isOnline(getActivity()))
  547. {
  548. AdRequest adRequest = new AdRequest.Builder()
  549. .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
  550. .addTestDevice(getString(R.string.admob_test_device_id))
  551. .build();
  552. adView.loadAd(adRequest);
  553. adView.setVisibility(View.VISIBLE);
  554. }
  555. else
  556. {
  557. adView.setVisibility(View.GONE);
  558. }
  559. }
  560.  
  561.  
  562. private void controlBack()
  563. {
  564. final WebView webView = (WebView) mRootView.findViewById(R.id.fragment_main_webview);
  565. if(webView.canGoBack()) webView.goBack();
  566. }
  567.  
  568.  
  569. private void controlForward()
  570. {
  571. final WebView webView = (WebView) mRootView.findViewById(R.id.fragment_main_webview);
  572. if(webView.canGoForward()) webView.goForward();
  573. }
  574.  
  575.  
  576. private void controlStop()
  577. {
  578. final WebView webView = (WebView) mRootView.findViewById(R.id.fragment_main_webview);
  579. webView.stopLoading();
  580. }
  581.  
  582.  
  583. private void controlReload()
  584. {
  585. final WebView webView = (WebView) mRootView.findViewById(R.id.fragment_main_webview);
  586. webView.reload();
  587. }
  588.  
  589.  
  590. private void startWebActivity(String url)
  591. {
  592. try
  593. {
  594. Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
  595. startActivity(intent);
  596. }
  597. catch(ActivityNotFoundException e)
  598. {
  599. // can't start activity
  600. }
  601. }
  602.  
  603.  
  604. private void startEmailActivity(String email, String subject, String text)
  605. {
  606. try
  607. {
  608. StringBuilder builder = new StringBuilder();
  609. builder.append("mailto:");
  610. builder.append(email);
  611.  
  612. Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(builder.toString()));
  613. intent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
  614. intent.putExtra(android.content.Intent.EXTRA_TEXT, text);
  615. startActivity(intent);
  616. }
  617. catch(ActivityNotFoundException e)
  618. {
  619. // can't start activity
  620. }
  621. }
  622.  
  623.  
  624. private void startCallActivity(String url)
  625. {
  626. try
  627. {
  628. Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
  629. startActivity(intent);
  630. }
  631. catch(ActivityNotFoundException e)
  632. {
  633. // can't start activity
  634. }
  635. }
  636.  
  637.  
  638. private void startSmsActivity(String url)
  639. {
  640. try
  641. {
  642. Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(url));
  643. startActivity(intent);
  644. }
  645. catch(ActivityNotFoundException e)
  646. {
  647. // can't start activity
  648. }
  649. }
  650.  
  651.  
  652. private void startMapSearchActivity(String url)
  653. {
  654. try
  655. {
  656. Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(url));
  657. startActivity(intent);
  658. }
  659. catch(android.content.ActivityNotFoundException e)
  660. {
  661. // can't start activity
  662. }
  663. }
  664.  
  665.  
  666. private void startShareActivity(String subject, String text)
  667. {
  668. try
  669. {
  670. Intent intent = new Intent(android.content.Intent.ACTION_SEND);
  671. intent.setType("text/plain");
  672. intent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
  673. intent.putExtra(android.content.Intent.EXTRA_TEXT, text);
  674. startActivity(intent);
  675. }
  676. catch(android.content.ActivityNotFoundException e)
  677. {
  678. // can't start activity
  679. }
  680. }
  681. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement