Advertisement
ansh_pastebin

Untitled

Sep 6th, 2013
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.68 KB | None | 0 0
  1. public class MainActivity extends Activity {
  2.     protected LinearLayout webViewPlaceholder;
  3.     protected WebView webView;
  4.     private LinearLayout mContentView;
  5.     private MyWebChromeClient mWebChromeClient = null;
  6.     private View mCustomView;
  7.     private FrameLayout mCustomViewContainer;
  8.     private WebChromeClient.CustomViewCallback mCustomViewCallback;
  9.     private boolean isFullScreen = false;
  10.      Button b;
  11.     FrameLayout.LayoutParams LayoutParameters = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
  12.             FrameLayout.LayoutParams.MATCH_PARENT);
  13.     @Override
  14.     protected void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.activity_main);
  17.        
  18.         System.out.println("Oncreate is getting called ---------------------------");
  19.         // Initialize the UI
  20.         if(savedInstanceState == null){
  21.             initUI();  
  22.         }
  23.        
  24.       }
  25.        
  26.       protected void initUI()
  27.       {
  28.         // Retrieve UI elements
  29.         webViewPlaceholder = ((LinearLayout)findViewById(R.id.webViewPlaceholder));
  30.         mContentView = webViewPlaceholder;
  31.         // Initialize the WebView if necessary
  32.        
  33.        
  34.         if (webView == null)
  35.         {
  36.             System.out.println("webView == null -----------------------------------");
  37.           // Create the webview
  38.           webView = new WebView(this);
  39.           LinearLayout.LayoutParams webViewLayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
  40.                   LinearLayout.LayoutParams.WRAP_CONTENT);
  41.          
  42.           webView.setLayoutParams(webViewLayoutParameters);
  43.           webView.setBackgroundColor(Color.BLUE);
  44.           WebSettings webSettings = webView.getSettings();
  45.           webSettings.setJavaScriptEnabled(true);
  46.             webSettings.setDomStorageEnabled(true);
  47.             webSettings.setAllowContentAccess(true);
  48.             webSettings.setDomStorageEnabled(true);
  49.             webSettings.setRenderPriority(RenderPriority.HIGH);    
  50.             webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
  51.             webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
  52.             if (Build.VERSION.SDK_INT < 8) {
  53.                 webSettings.setPluginsEnabled(true);
  54.             } else {
  55.                 webSettings.setPluginState(PluginState.ON);
  56.             }
  57.           mWebChromeClient = new MyWebChromeClient();
  58.           // Load a page
  59.           loadWebView("asfas");
  60.         }
  61.        
  62.         // Attach the WebView to its placeholder
  63.         webViewPlaceholder.addView(webView);
  64.         webView.setWebChromeClient(mWebChromeClient);
  65.       }
  66.      
  67.       private void loadWebView(final String url) {
  68.             String s = ReadFromfile("link.txt", MainActivity.this);
  69.             s ="<html><head><body>"+s+"</body></html>";
  70.             webView.loadDataWithBaseURL("",Html.fromHtml(s).toString(),"text/html", "UTF-8",null);
  71.         }
  72.        
  73.         public String ReadFromfile(String fileName, Context context) {
  74.             StringBuilder ReturnString = new StringBuilder();
  75.             InputStream fIn = null;
  76.             InputStreamReader isr = null;
  77.             BufferedReader input = null;
  78.             try {
  79.                 fIn = context.getResources().getAssets()
  80.                         .open(fileName, context.MODE_WORLD_READABLE);
  81.                 isr = new InputStreamReader(fIn);
  82.                 input = new BufferedReader(isr);
  83.                 String line = "";
  84.                 while ((line = input.readLine()) != null) {
  85.                     ReturnString.append(line);
  86.                 }
  87.             } catch (Exception e) {
  88.                 e.getMessage();
  89.             } finally {
  90.                 try {
  91.                     if (isr != null)
  92.                         isr.close();
  93.                     if (fIn != null)
  94.                         fIn.close();
  95.                     if (input != null)
  96.                         input.close();
  97.                 } catch (Exception e2) {
  98.                     e2.getMessage();
  99.                 }
  100.             }
  101.             return ReturnString.toString();
  102.         }
  103.        
  104.      
  105.       @Override
  106.       public void onConfigurationChanged(Configuration newConfig)
  107.       {
  108.         super.onConfigurationChanged(newConfig);       
  109.       }
  110.        
  111.       @Override
  112.       protected void onSaveInstanceState(Bundle outState)
  113.       {
  114.         super.onSaveInstanceState(outState);
  115.         System.out.println("onSaveInstanceState -------------------------");   
  116.         // Save the state of the WebView
  117.         webView.saveState(outState);
  118.       }
  119.        
  120.       @Override
  121.       protected void onRestoreInstanceState(Bundle savedInstanceState)
  122.       {
  123.         super.onRestoreInstanceState(savedInstanceState);
  124.         System.out.println("onRestoreInstanceState -------------------------");
  125.         // Restore the state of the WebView
  126.         webView.restoreState(savedInstanceState);
  127.       }
  128.       private class MyWebChromeClient extends WebChromeClient implements OnCompletionListener,android.media.MediaPlayer.OnErrorListener{
  129.            
  130.             @Override
  131.             public void onShowCustomView(View view, CustomViewCallback callback) {
  132.                 // if a view already exists then immediately terminate the new one
  133.                  System.out.println("onShowCustomView -------------------------"); 
  134.                 if (mCustomView != null) {
  135.                     callback.onCustomViewHidden();
  136.                     return;
  137.                 }
  138.                 isFullScreen = true;
  139.                 mContentView.setVisibility(View.GONE);
  140.                 mCustomViewContainer = new FrameLayout(MainActivity.this);
  141.                 mCustomViewContainer.setLayoutParams(LayoutParameters);
  142.                 mCustomViewContainer.setBackgroundResource(android.R.color.black);
  143.                 view.setLayoutParams(LayoutParameters);
  144.                 // Sometimes getting remove view first it parent excepetion on moto devices
  145.                 //webViewPlaceholder.removeView(webView);
  146.                // webViewPlaceholder.removeView(webView);
  147.                 mCustomViewContainer.addView(view);
  148.                 mCustomView = view;
  149.                 mCustomViewCallback = callback;
  150.                 mCustomViewContainer.setVisibility(View.VISIBLE);
  151.                 setContentView(mCustomViewContainer);
  152.             }
  153.  
  154.            
  155.             @Override
  156.             public void onHideCustomView() {
  157.                 isFullScreen = false;
  158.                 if (mCustomView == null) {
  159.                     return;
  160.                 } else {
  161.                     System.out.println("Hiding the things properly :-------------------------------");
  162.                     // Hide the custom view.  
  163.                     mCustomView.setVisibility(View.GONE);
  164.                     // Remove the custom view from its container.  
  165.                     if(mContentView.getParent() != null){
  166.                         ((LinearLayout)mContentView.getParent()).removeAllViews();
  167.                     }
  168.                   // mCustomViewContainer.removeView(mCustomView);
  169.                     mCustomView = null;
  170.                     mCustomViewContainer.setVisibility(View.GONE);
  171.                     mCustomViewCallback.onCustomViewHidden();
  172.                     // Show the content view.  
  173.                     mContentView.setVisibility(View.VISIBLE);
  174.                     setContentView(mContentView);//////
  175.                 }
  176.             }
  177.  
  178.  
  179.             @Override
  180.             public void onCompletion(MediaPlayer mp) {
  181.                 System.out.println("onCompletion :---------------------------");
  182.                
  183.             }
  184.  
  185.  
  186.             @Override
  187.             public boolean onError(MediaPlayer mp, int what, int extra) {
  188.                 Log.e("WebView","onError :---------------------------");
  189.                 return false;
  190.             }
  191.            
  192.         }
  193.  
  194.      @Override
  195.     public boolean onKeyDown(int keyCode, KeyEvent event) {
  196.          if(keyCode == KeyEvent.KEYCODE_BACK){
  197.              Log.e("WebView","back pressed :---------------------------");
  198.              if (mCustomViewContainer != null && isFullScreen){
  199.                  mWebChromeClient.onHideCustomView();
  200.              return true;
  201.              }
  202.          }else if(keyCode == KeyEvent.KEYCODE_HOME){
  203.              Log.e("WebView","home pressed :---------------------------");
  204.              mWebChromeClient.onHideCustomView();
  205.          }
  206.          return super.onKeyDown(keyCode, event);
  207.     }
  208.      
  209.      /* @Override
  210.         public void onBackPressed() {
  211.             if (mCustomViewContainer != null)
  212.                 mWebChromeClient.onHideCustomView();
  213.             else
  214.                 super.onBackPressed();
  215.         }*/
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement