<commit_before>package com.alibaba.weex;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.weex.constants.Constants;
import com.alibaba.weex.https.HotRefreshManager;
import com.alibaba.weex.https.WXHttpManager;
import com.alibaba.weex.https.WXHttpTask;
import com.alibaba.weex.https.WXRequestListener;
import com.alibaba.weex.util.ScreenUtil;
import com.taobao.weex.IWXRenderListener;
import com.taobao.weex.WXEnvironment;
import com.taobao.weex.WXSDKInstance;
import com.taobao.weex.common.WXRenderStrategy;
import com.taobao.weex.utils.WXFileUtils;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;


public class WXPageActivity extends WXBaseActivity implements IWXRenderListener, android.os.Handler.Callback {

  private static final String TAG = "WXPageActivity";
  public static Activity wxPageActivityInstance;

  private ViewGroup mContainer;
  private ProgressBar mProgressBar;
  private View mWAView;

  private WXSDKInstance mInstance;
  private Handler mWXHandler;

  private Uri mUri;
  private HashMap mConfigMap = new HashMap<String, Object>();

  private Button mRefresh;
  private JSONArray mData;
  private int count = 0;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wxpage);

    //        wxPageActivityInstance = this;
    setCurrentWxPageActivity(this);

    mUri = getIntent().getData();
    Bundle bundle = getIntent().getExtras();
    if (mUri == null && bundle == null) {
      mUri = Uri.parse(Constants.BUNDLE_URL + Constants.WEEX_SAMPLES_KEY);
    }
    if (bundle != null) {
      String bundleUrl = bundle.getString("bundleUrl");
      Log.e(TAG, "bundleUrl==" + bundleUrl);

      if (bundleUrl != null) {
        mConfigMap.put("bundleUrl", bundleUrl + Constants.WEEX_SAMPLES_KEY);
        mUri = Uri.parse(bundleUrl + Constants.WEEX_SAMPLES_KEY);

      }
    } else {
      mConfigMap.put("bundleUrl", mUri.toString() + Constants.WEEX_SAMPLES_KEY);
      //            mUri = Uri.parse(mUri.toString() + Constants.WEEX_SAMPLES_KEY)
    }

    if (!WXEnvironment.isSupport()) {
      return;
    }

    if (mUri == null) {
      Toast.makeText(this, "the uri is empty!", Toast.LENGTH_SHORT).show();
      finish();
      return;
    }

    Log.e("TestScript_Guide mUri==", mUri.toString());
//    mData = (JSONArray) JSONArray.parse(WXFileUtils.loadFileContent("mockShopMtop.json", this));

    //        mRefresh = (Button) findViewById(R.id.weex_refresh);

    //        mRefresh.setOnClickListener(new View.OnClickListener() {
    //            @Override
    //            public void onClick(View v) {
    //                if (count < mData.size()) {
    //                    mInstance.refreshInstance(((JSONObject) mData.get(count)).toJSONString());
    //                    count++;
    //                }
    //            }
    //        });

    initUIAndData();

    if (TextUtils.equals("http", mUri.getScheme()) || TextUtils.equals("https", mUri.getScheme())) {
      loadWXfromService(mUri.toString());
      startHotRefresh();
    } else {
      if (mInstance == null) {
        mInstance = new WXSDKInstance(this);
//        mInstance.setImgLoaderAdapter(new ImageAdapter(this));
        mInstance.registerRenderListener(this);
      }
      Activity ctx = this;
      Rect outRect = new Rect();
      ctx.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
      String path=mUri.getScheme().equals("file")?mUri.getLastPathSegment():mUri.toString();
      mInstance.render(TAG, WXFileUtils.loadFileContent(path, this), mConfigMap, null, ScreenUtil.getDisplayWidth(this), ScreenUtil.getDisplayHeight(this), WXRenderStrategy.APPEND_ASYNC);
    }
    mInstance.onActivityCreate();
  }

  private void initUIAndData() {

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setTitle(mUri.toString().substring(mUri.toString().lastIndexOf(File.separator) + 1));

    mContainer = (ViewGroup) findViewById(R.id.container);
    mProgressBar = (ProgressBar) findViewById(R.id.progress);
    mWXHandler = new Handler(this);
    HotRefreshManager.getInstance().setHandler(mWXHandler);
    addOnListener();
  }

  private void loadWXfromService(final String url) {
    mProgressBar.setVisibility(View.VISIBLE);

    if (mInstance != null) {
      mInstance.destroy();
    }

    mInstance = new WXSDKInstance(this);
//    mInstance.setImgLoaderAdapter(new ImageAdapter(this));

    mInstance.registerRenderListener(this);

    WXHttpTask httpTask = new WXHttpTask();
    httpTask.url = url;
    httpTask.requestListener = new WXRequestListener() {

      @Override
      public void onSuccess(WXHttpTask task) {
        Log.e(TAG, "into--[http:onSuccess] url:" + url);
        try {
          mConfigMap.put("bundleUrl", url + Constants.WEEX_SAMPLES_KEY);
          mInstance.render(TAG, new String(task.response.data, "utf-8"), mConfigMap, null, mContainer.getWidth(), mContainer.getHeight(), WXRenderStrategy.APPEND_ASYNC);

          //                    mInstance.render(new String(task.response.data, "utf-8"), mContainer.getWidth(), mContainer.getHeight());
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        }
      }

      @Override
      public void onError(WXHttpTask task) {
        Log.e(TAG, "into--[http:onError]");
      }
    };

    WXHttpManager.getInstance().sendRequest(httpTask);
  }

  /**
   * hot refresh
   */
  private void startHotRefresh() {
    try {<commit_msg>* [android] update WXPageActivity.java modify container width and height<commit_after>package com.alibaba.weex;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.weex.constants.Constants;
import com.alibaba.weex.https.HotRefreshManager;
import com.alibaba.weex.https.WXHttpManager;
import com.alibaba.weex.https.WXHttpTask;
import com.alibaba.weex.https.WXRequestListener;
import com.alibaba.weex.util.ScreenUtil;
import com.taobao.weex.IWXRenderListener;
import com.taobao.weex.WXEnvironment;
import com.taobao.weex.WXSDKInstance;
import com.taobao.weex.common.WXRenderStrategy;
import com.taobao.weex.utils.WXFileUtils;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;


public class WXPageActivity extends WXBaseActivity implements IWXRenderListener, android.os.Handler.Callback {

  private static final String TAG = "WXPageActivity";
  public static Activity wxPageActivityInstance;

  private ViewGroup mContainer;
  private ProgressBar mProgressBar;
  private View mWAView;

  private WXSDKInstance mInstance;
  private Handler mWXHandler;

  private Uri mUri;
  private HashMap mConfigMap = new HashMap<String, Object>();

  private Button mRefresh;
  private JSONArray mData;
  private int count = 0;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wxpage);

    //        wxPageActivityInstance = this;
    setCurrentWxPageActivity(this);

    mUri = getIntent().getData();
    Bundle bundle = getIntent().getExtras();
    if (mUri == null && bundle == null) {
      mUri = Uri.parse(Constants.BUNDLE_URL + Constants.WEEX_SAMPLES_KEY);
    }
    if (bundle != null) {
      String bundleUrl = bundle.getString("bundleUrl");
      Log.e(TAG, "bundleUrl==" + bundleUrl);

      if (bundleUrl != null) {
        mConfigMap.put("bundleUrl", bundleUrl + Constants.WEEX_SAMPLES_KEY);
        mUri = Uri.parse(bundleUrl + Constants.WEEX_SAMPLES_KEY);

      }
    } else {
      mConfigMap.put("bundleUrl", mUri.toString() + Constants.WEEX_SAMPLES_KEY);
      //            mUri = Uri.parse(mUri.toString() + Constants.WEEX_SAMPLES_KEY)
    }

    if (!WXEnvironment.isSupport()) {
      return;
    }

    if (mUri == null) {
      Toast.makeText(this, "the uri is empty!", Toast.LENGTH_SHORT).show();
      finish();
      return;
    }

    Log.e("TestScript_Guide mUri==", mUri.toString());
//    mData = (JSONArray) JSONArray.parse(WXFileUtils.loadFileContent("mockShopMtop.json", this));

    //        mRefresh = (Button) findViewById(R.id.weex_refresh);

    //        mRefresh.setOnClickListener(new View.OnClickListener() {
    //            @Override
    //            public void onClick(View v) {
    //                if (count < mData.size()) {
    //                    mInstance.refreshInstance(((JSONObject) mData.get(count)).toJSONString());
    //                    count++;
    //                }
    //            }
    //        });

    initUIAndData();

    if (TextUtils.equals("http", mUri.getScheme()) || TextUtils.equals("https", mUri.getScheme())) {
      loadWXfromService(mUri.toString());
      startHotRefresh();
    } else {
      if (mInstance == null) {
        mInstance = new WXSDKInstance(this);
//        mInstance.setImgLoaderAdapter(new ImageAdapter(this));
        mInstance.registerRenderListener(this);
      }
      Activity ctx = this;
      Rect outRect = new Rect();
      ctx.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
      String path=mUri.getScheme().equals("file")?mUri.getLastPathSegment():mUri.toString();
      mInstance.render(TAG, WXFileUtils.loadFileContent(path, this), mConfigMap, null, ScreenUtil.getDisplayWidth(this), ScreenUtil.getDisplayHeight(this), WXRenderStrategy.APPEND_ASYNC);
    }
    mInstance.onActivityCreate();
  }

  private void initUIAndData() {

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setTitle(mUri.toString().substring(mUri.toString().lastIndexOf(File.separator) + 1));

    mContainer = (ViewGroup) findViewById(R.id.container);
    mProgressBar = (ProgressBar) findViewById(R.id.progress);
    mWXHandler = new Handler(this);
    HotRefreshManager.getInstance().setHandler(mWXHandler);
    addOnListener();
  }

  private void loadWXfromService(final String url) {
    mProgressBar.setVisibility(View.VISIBLE);

    if (mInstance != null) {
      mInstance.destroy();
    }

    mInstance = new WXSDKInstance(this);
//    mInstance.setImgLoaderAdapter(new ImageAdapter(this));

    mInstance.registerRenderListener(this);

    WXHttpTask httpTask = new WXHttpTask();
    httpTask.url = url;
    httpTask.requestListener = new WXRequestListener() {

      @Override
      public void onSuccess(WXHttpTask task) {
        Log.e(TAG, "into--[http:onSuccess] url:" + url);
        try {
          mConfigMap.put("bundleUrl", url + Constants.WEEX_SAMPLES_KEY);
          mInstance.render(TAG, new String(task.response.data, "utf-8"), mConfigMap, null, ScreenUtil.getDisplayWidth(WXPageActivity.this), ScreenUtil.getDisplayHeight(WXPageActivity.this), WXRenderStrategy.APPEND_ASYNC);

          //                    mInstance.render(new String(task.response.data, "utf-8"), mContainer.getWidth(), mContainer.getHeight());
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        }
      }

      @Override
      public void onError(WXHttpTask task) {
        Log.e(TAG, "into--[http:onError]");
      }
    };

    WXHttpManager.getInstance().sendRequest(httpTask);
  }

  /**
   * hot refresh
   */
  private void startHotRefresh() {
    try {
{'fraction_non_alphanumeric': 0.12174186943853847, 'fraction_numerical': 0.0009449562957713206, 'mean_word_length': 3.44989488437281, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5529335', 'n_tokens_mistral': 3976, 'n_tokens_neox': 3851, 'n_words': 690}