Upstream version 8.36.171.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core_shell / src / org / xwalk / core / xwview / shell / XWalkViewShellActivity.java
index a408c17..3bcbd11 100644 (file)
@@ -1,16 +1,24 @@
-// Copyright (c) 2013 Intel Corporation. All rights reserved.
+// Copyright (c) 2013-2014 Intel Corporation. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
 package org.xwalk.core.xwview.shell;
 
-import android.app.Activity;
+import java.util.HashMap;
+
+import android.app.ActionBar;
+import android.app.ActionBar.Tab;
+import android.app.FragmentTransaction;
+import android.content.BroadcastReceiver;
 import android.content.Intent;
 import android.content.Context;
+import android.content.IntentFilter;
 import android.graphics.drawable.ClipDrawable;
 import android.os.Bundle;
 import android.os.Looper;
 import android.os.MessageQueue;
+import android.support.v4.app.FragmentActivity;
+import android.support.v4.view.ViewPager;
 import android.util.Log;
 import android.view.KeyEvent;
 import android.view.View;
@@ -24,17 +32,23 @@ import android.widget.LinearLayout;
 import android.widget.TextView;
 import android.widget.TextView.OnEditorActionListener;
 
-import org.chromium.content.app.LibraryLoader;
+import org.chromium.base.BaseSwitches;
+import org.chromium.base.CommandLine;
+import org.chromium.base.library_loader.LibraryLoader;
 import org.chromium.content.browser.TracingControllerAndroid;
-import org.chromium.content.common.CommandLine;
-import org.xwalk.core.client.XWalkDefaultWebChromeClient;
+import org.xwalk.core.XWalkNavigationHistory;
+import org.xwalk.core.XWalkPreferences;
+import org.xwalk.core.XWalkResourceClient;
+import org.xwalk.core.XWalkUIClient;
 import org.xwalk.core.XWalkView;
 
-public class XWalkViewShellActivity extends Activity {
+public class XWalkViewShellActivity extends FragmentActivity
+        implements ActionBar.TabListener, XWalkViewSectionFragment.OnXWalkViewCreatedListener {
     public static final String COMMAND_LINE_FILE = "/data/local/tmp/xwview-shell-command-line";
     private static final String TAG = XWalkViewShellActivity.class.getName();
     public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs";
     private static final long COMPLETED_PROGRESS_TIMEOUT_MS = 200;
+    private static final String ACTION_LAUNCH_URL = "org.xwalk.core.xwview.shell.launch";
 
     private LinearLayout mToolbar;
     private EditText mUrlTextView;
@@ -43,8 +57,13 @@ public class XWalkViewShellActivity extends Activity {
     private ImageButton mStopButton;
     private ImageButton mReloadButton;
     private ClipDrawable mProgressDrawable;
-    private XWalkView mView;
+    private XWalkView mActiveView;
     private TracingControllerAndroid mTracingController;
+    private BroadcastReceiver mReceiver;
+    private ActionBar mActionBar;
+    private SectionsPagerAdapter mSectionsPagerAdapter;
+    private ViewPager mViewPager;
+    private HashMap<XWalkView, Integer> mProgressMap;
 
     private Runnable mClearProgressRunnable = new Runnable() {
         @Override
@@ -82,6 +101,8 @@ public class XWalkViewShellActivity extends Activity {
             getTracingController().unregisterReceiver(this);
         } catch (SecurityException e) {
             Log.w(TAG, "failed to unregister tracing receiver: " + e.getMessage());
+        } catch (IllegalArgumentException e) {
+            Log.w(TAG, "failed to unregister tracing receiver: " + e.getMessage());
         }
     }
 
@@ -103,48 +124,72 @@ public class XWalkViewShellActivity extends Activity {
 
         setContentView(R.layout.testshell_activity);
 
-        mView = (XWalkView) findViewById(R.id.content_container);
-        mToolbar = (LinearLayout) findViewById(R.id.toolbar);
-        mProgressDrawable = (ClipDrawable) findViewById(R.id.toolbar).getBackground();
+        mActionBar = getActionBar();
+        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
+        mSectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager(), mActionBar);
 
-        initializeUrlField();
-        initializeButtons();
-        initializeXWalkViewClients();
+        mViewPager = (ViewPager) findViewById(R.id.pager);
+        mViewPager.setAdapter(mSectionsPagerAdapter);
+        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
+            @Override
+            public void onPageSelected(int position) {
+                mActionBar.setSelectedNavigationItem(position);
+            }
+        });
 
-        mView.enableRemoteDebugging();
-    }
+        mProgressMap = new HashMap<XWalkView, Integer>();
+        // Add two tabs.
+        mActionBar.addTab(
+                mActionBar.newTab()
+                        .setText(mSectionsPagerAdapter.getPageTitle(0))
+                        .setTabListener(this));
+        mActionBar.addTab(
+                mActionBar.newTab()
+                        .setText(mSectionsPagerAdapter.getPageTitle(1))
+                        .setTabListener(this));
 
-    @Override
-    public void onPause() {
-        super.onPause();
-        mView.onPause();
-    }
+        mToolbar = (LinearLayout) findViewById(R.id.toolbar);
+        mProgressDrawable = (ClipDrawable) findViewById(R.id.toolbar).getBackground();
 
-    @Override
-    public void onResume() {
-        super.onResume();
-        mView.onResume();
+        IntentFilter intentFilter = new IntentFilter(ACTION_LAUNCH_URL);
+        mReceiver = new BroadcastReceiver() {
+            @Override
+            public void onReceive(Context context, Intent intent) {
+                Bundle bundle = intent.getExtras();
+                if (bundle == null)
+                    return;
+
+                if (bundle.containsKey("url")) {
+                    String extra = bundle.getString("url");
+                    if (mActiveView != null)
+                        mActiveView.load(sanitizeUrl(extra), null);
+                }
+            }
+        };
+        registerReceiver(mReceiver, intentFilter);
     }
 
     @Override
     public void onDestroy() {
         super.onDestroy();
+        unregisterReceiver(mReceiver);
         unregisterTracingReceiver();
-        mView.onDestroy();
     }
 
     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
-        mView.onActivityResult(requestCode, resultCode, data);
+        if (mActiveView != null) mActiveView.onActivityResult(requestCode, resultCode, data);
     }
 
     @Override
-    public boolean onKeyUp(int keyCode, KeyEvent event) {
-        return mView.onKeyUp(keyCode, event) || super.onKeyUp(keyCode, event);
+    public void onNewIntent(Intent intent) {
+        if (mActiveView != null) {
+            if (!mActiveView.onNewIntent(intent)) super.onNewIntent(intent);
+        }
     }
 
     private void waitForDebuggerIfNeeded() {
-        if (CommandLine.getInstance().hasSwitch(CommandLine.WAIT_FOR_JAVA_DEBUGGER)) {
+        if (CommandLine.getInstance().hasSwitch(BaseSwitches.WAIT_FOR_JAVA_DEBUGGER)) {
             Log.e(TAG, "Waiting for Java debugger to connect...");
             android.os.Debug.waitForDebugger();
             Log.e(TAG, "Java debugger connected. Resuming execution.");
@@ -172,7 +217,8 @@ public class XWalkViewShellActivity extends Activity {
                     return false;
                 }
 
-                mView.loadUrl(sanitizeUrl(mUrlTextView.getText().toString()));
+                if (mActiveView == null) return true;
+                mActiveView.load(sanitizeUrl(mUrlTextView.getText().toString()), null);
                 mUrlTextView.clearFocus();
                 setKeyboardVisibilityForUrl(false);
                 return true;
@@ -187,11 +233,11 @@ public class XWalkViewShellActivity extends Activity {
                 mStopButton.setVisibility(hasFocus ? View.GONE : View.VISIBLE);
                 mReloadButton.setVisibility(hasFocus ? View.GONE : View.VISIBLE);
                 if (!hasFocus) {
-                    mUrlTextView.setText(mView.getUrl());
+                    if (mActiveView == null) return;
+                    mUrlTextView.setText(mActiveView.getUrl());
                 }
             }
         });
-
     }
 
     private void initializeButtons() {
@@ -199,7 +245,11 @@ public class XWalkViewShellActivity extends Activity {
         mPrevButton.setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View v) {
-                if (mView.canGoBack()) mView.goBack();
+                if (mActiveView != null &&
+                        mActiveView.getNavigationHistory().canGoBack()) {
+                    mActiveView.getNavigationHistory().navigate(
+                            XWalkNavigationHistory.Direction.BACKWARD, 1);
+                }
             }
         });
 
@@ -207,7 +257,11 @@ public class XWalkViewShellActivity extends Activity {
         mNextButton.setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View v) {
-                if (mView.canGoForward()) mView.goForward();
+                if (mActiveView != null &&
+                        mActiveView.getNavigationHistory().canGoForward()) {
+                    mActiveView.getNavigationHistory().navigate(
+                            XWalkNavigationHistory.Direction.FORWARD, 1);
+                }
             }
         });
 
@@ -215,7 +269,7 @@ public class XWalkViewShellActivity extends Activity {
         mStopButton.setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View v) {
-                mView.stopLoading();
+                if (mActiveView != null) mActiveView.stopLoading();
             }
         });
 
@@ -223,23 +277,36 @@ public class XWalkViewShellActivity extends Activity {
         mReloadButton.setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View v) {
-                mView.reload();
+                if (mActiveView != null) mActiveView.reload(XWalkView.RELOAD_NORMAL);
             }
         });
     }
 
-    private void initializeXWalkViewClients() {
-        mView.setXWalkWebChromeClient(new XWalkDefaultWebChromeClient(this, mView) {
+    private void initializeXWalkViewClients(XWalkView xwalkView) {
+        xwalkView.setResourceClient(new XWalkResourceClient(xwalkView) {
+            @Override
             public void onProgressChanged(XWalkView view, int newProgress) {
+                if (view != mActiveView) return;
                 mToolbar.removeCallbacks(mClearProgressRunnable);
 
                 mProgressDrawable.setLevel((int) (100.0 * newProgress));
-                if (newProgress == 100)
+                mProgressMap.put(view, (int) (100.0 * newProgress));
+                if (newProgress == 100) {
                     mToolbar.postDelayed(mClearProgressRunnable, COMPLETED_PROGRESS_TIMEOUT_MS);
-                    mUrlTextView.setText(mView.getUrl());
+                    mProgressMap.put(view, 0);
+                }
+                mUrlTextView.setText(mActiveView.getUrl());
+            }
+        });
+
+        xwalkView.setUIClient(new XWalkUIClient(xwalkView) {
+            @Override
+            public void onReceivedTitle(XWalkView view, String title) {
+                mSectionsPagerAdapter.setPageTitle(view, title);
             }
         });
     }
+
     private void setKeyboardVisibilityForUrl(boolean visible) {
         InputMethodManager imm = (InputMethodManager) getSystemService(
                 Context.INPUT_METHOD_SERVICE);
@@ -249,4 +316,47 @@ public class XWalkViewShellActivity extends Activity {
             imm.hideSoftInputFromWindow(mUrlTextView.getWindowToken(), 0);
         }
     }
+
+    @Override
+    public void onTabReselected(Tab tab, FragmentTransaction ft) {
+        // Do nothing here currently, just make compiler happy.
+    }
+
+    @Override
+    public void onTabSelected(Tab tab, FragmentTransaction ft) {
+        mViewPager.setCurrentItem(tab.getPosition());
+        android.support.v4.app.Fragment fragment = mSectionsPagerAdapter.getItem(tab.getPosition());
+        if (fragment!= null && fragment instanceof XWalkViewSectionFragment) {
+            mActiveView = ((XWalkViewSectionFragment)fragment).getXWalkView();
+        } else {
+            mActiveView = null;
+        }
+        if (mActiveView != null) {
+            mUrlTextView.setText(mActiveView.getUrl());
+            if (mProgressMap.containsKey(mActiveView)) {
+                mProgressDrawable.setLevel(mProgressMap.get(mActiveView));
+            } else {
+                mProgressDrawable.setLevel(0);
+            }
+        }
+    }
+
+    @Override
+    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
+        // Do nothing here currently, just make compiler happy.
+    }
+
+    @Override
+    public void onXWalkViewCreated(XWalkView view) {
+        if (mActiveView == null) {
+            mActiveView = view;
+            initializeUrlField();
+            initializeButtons();
+            mUrlTextView.setText("");
+            mProgressDrawable.setLevel(0);
+        }
+        initializeXWalkViewClients(view);
+        mProgressMap.put(view, 0);
+        XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGING, true);
+    }
 }