@Override
 public boolean onOptionsItemSelected(MenuItem item) {
   // Handle action bar item clicks here. The action bar will
   // automatically handle clicks on the Home/Up button, so long
   // as you specify a parent activity in AndroidManifest.xml.  }   int id = item.getItemId();
   switch (item.getItemId()) {
       // Respond to the action bar's Up/Home button
     case android.R.id.home:
       Intent intent = NavUtils.getParentActivityIntent(this);
       intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
       startActivity(intent);
       overridePendingTransition(0, R.anim.slide_out_right);
       return true;
     case R.id.menu_item_picture:
       mWebView.loadUrl(
           "javascript:document.getElementsByClassName('jianshu_bar')[0].style.display = 'block'");
       this.handler.postDelayed(
           new Runnable() {
             @Override
             public void run() {
               scanContent();
             }
           },
           1000);
       this.scanLight.setVisibility(View.VISIBLE);
       this.scanLight.startAnimation(this.scanAnim);
       return true;
   }
   return super.onOptionsItemSelected(item);
 }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_article);

    this.handler = new Handler();

    Intent intent = getIntent();
    mUrl = intent.getStringExtra("url");
    //    mTitle = intent.getStringExtra("title");
    //    mSummary = intent.getStringExtra("summary");
    //    mAuthor = intent.getStringExtra("author");
    mLoadingArticle = (LoadingTextView) findViewById(R.id.loading_article);
    mWebView = (ObservableWebView) findViewById(R.id.web);
    mWebView.setOnScrollChangedCallback(this);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.addJavascriptInterface(this, "article");
    mRetryButton = (Button) findViewById(R.id.retry);
    this.scanLight = (View) findViewById(R.id.scan_light);

    final ArticleActivity that = this;

    this.fadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in);
    this.fadeOut = AnimationUtils.loadAnimation(this, R.anim.fade_out);
    this.fadeOut.setAnimationListener(
        new Animation.AnimationListener() {
          @Override
          public void onAnimationStart(Animation animation) {}

          @Override
          public void onAnimationEnd(Animation animation) {
            ArticleActivity.this.mLikeView.setVisibility(View.GONE);
          }

          @Override
          public void onAnimationRepeat(Animation animation) {}
        });

    this.scanAnim = AnimationUtils.loadAnimation(this, R.anim.scan);
    this.scanAnim.setAnimationListener(
        new Animation.AnimationListener() {

          @Override
          public void onAnimationStart(Animation animation) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            that.scanFinishedDialogFragment = ScanFinishedDialogFragment.newInstance();
            that.scanFinishedDialogFragment.show(ft, "scan");
          }

          @Override
          public void onAnimationEnd(Animation animation) {
            ArticleActivity.this.scanLight.setVisibility(View.GONE);
          }

          @Override
          public void onAnimationRepeat(Animation animation) {}
        });

    mLikeProgress = (ProgressBar) findViewById(R.id.like_progress);
    this.likingAnim =
        ObjectAnimator.ofInt(this.mLikeProgress, "progress", 2, mLikeProgress.getMax() - 1);
    this.likingAnim.setDuration(2000);
    this.unlikingAnim =
        ObjectAnimator.ofInt(this.mLikeProgress, "progress", mLikeProgress.getMax() - 1, 2);
    this.unlikingAnim.setDuration(2000);

    mLikeView = findViewById(R.id.like);
    mLikeTextView = (TextView) findViewById(R.id.like_text);
    mLikeView.setOnClickListener(
        new View.OnClickListener() {

          @Override
          public void onClick(View v) {
            final ArticleActivity that = ArticleActivity.this;
            final int max = mLikeProgress.getMax();
            if (that.isLikingProgressing) {
              return;
            }
            that.isLikingProgressing = true;

            that.currentAnim = that.isLiking ? that.unlikingAnim : that.likingAnim;
            that.currentAnim.start();

            (new AsyncTask<Void, Void, Boolean>() {

                  @Override
                  protected Boolean doInBackground(Void... params) {
                    Object httpResult = JianshuSession.getsInstance().postSync(that.likeUrl, false);
                    if (httpResult instanceof String) {
                      String str = (String) httpResult;
                      if (str.startsWith("$")) {
                        if (str.contains("addClass('note-liked')")) {
                          that.isLiking = true;
                        } else if (str.contains("removeClass('note-liked')")) {
                          that.isLiking = false;
                        }
                        Matcher matcher = LIKE_COUNT_PATTERN.matcher(str);
                        if (matcher.find()) {
                          that.likingCount = Integer.parseInt(matcher.group(1));
                        }
                        return true;
                      }
                    }
                    return false;
                  }

                  @Override
                  protected void onPostExecute(Boolean succeed) {
                    that.isLikingProgressing = false;
                    // 即使是网络问题失败了,也要重置进度条状态
                    updateLike();
                    if (!succeed) {
                      Toast.makeText(that, "网络似乎不给力", Toast.LENGTH_LONG).show();
                    }
                  }
                })
                .execute();
          }
        });

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);

    // 滑动返回
    mSwipeBackLayout = getSwipeBackLayout();
    mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);

    mRetryButton.setOnClickListener(
        new Button.OnClickListener() {

          @Override
          public void onClick(View v) {
            mRetryButton.setVisibility(View.INVISIBLE);
            mLoadingArticle.setVisibility(View.VISIBLE);
            loadArticle();
          }
        });

    mDownloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

    mFinalHttp = new FinalHttp();

    Object[] state = StatePool.getInstance().getState("article");
    if (state != null) {
      String url = (String) state[0];
      if (mUrl.equals(url)) {
        this.mTitle = (String) state[1];
        this.mAuthor = (String) state[2];
        this.content = (String) state[3];
        mWebView.setVisibility(View.VISIBLE);
        mWebView.loadData(this.content, "text/html; charset=UTF-8", null);
        return;
      }
    }
    loadArticle();
  }
  public void scanContent() {
    int[] size = this.mWebView.getRealSize();
    int width = size[0];
    int height = size[1];
    try {
      this.scanBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
      Canvas canvas = new Canvas(this.scanBitmap);
      this.mWebView.draw(canvas);
    } catch (OutOfMemoryError e) {
      this.scanBitmap = null;
      mWebView.loadUrl(
          "javascript:document.getElementsByClassName('jianshu_bar')[0].style.display = 'none'");
      this.scanFinishedDialogFragment.onScanError("篇幅过长,不能扫描");
      return;
    }
    final ArticleActivity that = ArticleActivity.this;
    (new AsyncTask<Void, Void, Boolean>() {

          @Override
          protected Boolean doInBackground(Void... params) {
            try {
              File jianshuImageFile =
                  new File(
                      Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                          + "/jianshu");
              if (!jianshuImageFile.exists()) {
                jianshuImageFile.mkdirs();
              }
              that.imagePath =
                  Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                      + "/jianshu/"
                      + getImageFileName(mTitle);
              FileOutputStream fos = new FileOutputStream(that.imagePath);
              that.scanBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
              that.scanBitmap.recycle();
              that.scanBitmap = null;
              fos.close();
              return true;
            } catch (FileNotFoundException e) {
              e.printStackTrace();
            } catch (IOException e) {
              e.printStackTrace();
            }
            return false;
          }

          @Override
          protected void onPostExecute(Boolean succeed) {
            mWebView.loadUrl(
                "javascript:document.getElementsByClassName('jianshu_bar')[0].style.display = 'none'");
            if (succeed) {
              that.imageUri = Uri.fromFile(new File(that.imagePath));
              Intent localIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, that.imageUri);
              sendBroadcast(localIntent);
              that.scanFinishedDialogFragment.onScanFinished();
            } else {
              that.scanFinishedDialogFragment.onScanError("保存图片时遇到错误");
            }
          }
        })
        .execute();
  }