Exemple #1
0
  private String readResult(HttpURLConnection urlConnection) throws WeiboException {
    InputStream is = null;
    BufferedReader buffer = null;
    BeeboApplication globalContext = BeeboApplication.getInstance();
    String errorStr = globalContext.getString(R.string.timeout);
    globalContext = null;
    try {
      is = urlConnection.getInputStream();

      String content_encode = urlConnection.getContentEncoding();

      if (!TextUtils.isEmpty(content_encode) && content_encode.equals("gzip")) {
        is = new GZIPInputStream(is);
      }

      buffer = new BufferedReader(new InputStreamReader(is));
      StringBuilder strBuilder = new StringBuilder();
      String line;
      while ((line = buffer.readLine()) != null) {
        strBuilder.append(line);
      }
      // AppLogger.d("result=" + strBuilder.toString());
      return strBuilder.toString();
    } catch (IOException e) {
      e.printStackTrace();
      throw new WeiboException(errorStr, e);
    } finally {
      Utility.closeSilently(is);
      Utility.closeSilently(buffer);
      urlConnection.disconnect();
    }
  }
Exemple #2
0
  public String doGet(String urlStr, Map<String, String> param) throws WeiboException {
    BeeboApplication globalContext = BeeboApplication.getInstance();
    String errorStr = globalContext.getString(R.string.timeout);
    globalContext = null;
    InputStream is = null;
    try {

      StringBuilder urlBuilder = new StringBuilder(urlStr);
      urlBuilder.append("?").append(Utility.encodeUrl(param));
      URL url = new URL(urlBuilder.toString());
      AppLoggerUtils.d("get request" + url);
      Proxy proxy = getProxy();
      HttpURLConnection urlConnection;
      if (proxy != null) {
        urlConnection = (HttpURLConnection) url.openConnection(proxy);
      } else {
        urlConnection = (HttpURLConnection) url.openConnection();
      }

      urlConnection.setRequestMethod("GET");
      urlConnection.setDoOutput(false);
      urlConnection.setConnectTimeout(CONNECT_TIMEOUT);
      urlConnection.setReadTimeout(READ_TIMEOUT);
      urlConnection.setRequestProperty("Connection", "Keep-Alive");
      urlConnection.setRequestProperty("Charset", "UTF-8");
      urlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");

      urlConnection.connect();

      return handleResponse(urlConnection);
    } catch (IOException e) {
      e.printStackTrace();
      throw new WeiboException(errorStr, e);
    }
  }
  @Override
  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

    buildSummary();

    if (key.equals(SettingActivity.LIST_AVATAR_MODE)) {
      String value = sharedPreferences.getString(key, "1");
      if (value.equals("1")) SettingUtils.setEnableBigAvatar(false);
      if (value.equals("2")) SettingUtils.setEnableBigAvatar(true);
      if (value.equals("3")) {
        SettingUtils.setEnableBigAvatar(Utility.isWifi(getActivity()));
      }
    }

    if (key.equals(SettingActivity.LIST_PIC_MODE)) {
      String value = sharedPreferences.getString(key, "1");
      if (value.equals("1")) {
        SettingUtils.setEnableBigPic(false);
        // listHighPicMode.setEnabled(false);
      }
      if (value.equals("2")) {
        SettingUtils.setEnableBigPic(true);
        // listHighPicMode.setEnabled(true);
      }
      if (value.equals("3")) {
        SettingUtils.setEnableBigPic(Utility.isWifi(getActivity()));
        // listHighPicMode.setEnabled(true);
      }
    }
    if (key.equals(SettingActivity.LIST_HIGH_PIC_MODE)) {
      BeeboApplication.getInstance().getBitmapCache().evictAll();
    }
  }
Exemple #4
0
  private String handleResponse(HttpURLConnection httpURLConnection) throws WeiboException {
    BeeboApplication globalContext = BeeboApplication.getInstance();
    String errorStr = globalContext.getString(R.string.timeout);
    globalContext = null;
    int status = 0;
    try {
      status = httpURLConnection.getResponseCode();
    } catch (IOException e) {
      e.printStackTrace();
      httpURLConnection.disconnect();
      throw new WeiboException(errorStr, e);
    }

    if (status != HttpURLConnection.HTTP_OK) {
      return handleError(httpURLConnection);
    }

    return readResult(httpURLConnection);
  }
  private void handleNormalOperation(Intent intent) {

    token = intent.getStringExtra(Constants.TOKEN);
    if (TextUtils.isEmpty(token)) {
      token = BeeboApplication.getInstance().getAccessToken();
    }

    bean = intent.getParcelableExtra("msg");
    getEditTextView().setHint("@" + bean.getUser().getScreen_name() + ":" + bean.getText());
  }
 @Override
 public void saveToDraft() {
   if (!TextUtils.isEmpty(getEditTextView().getText().toString())) {
     DraftDBManager.getInstance()
         .insertReply(
             getEditTextView().getText().toString(),
             bean,
             BeeboApplication.getInstance().getCurrentAccountId());
   }
   finish();
 }
 @Override
 protected void onNewMsgLoaderSuccessCallback(CommentListBean newValue, Bundle loaderArgs) {
   if (newValue != null && newValue.getItemList() != null && newValue.getItemList().size() > 0) {
     addNewDataAndRememberPosition(newValue);
   }
   unreadBean = null;
   NotificationManager notificationManager =
       (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
   notificationManager.cancel(
       NotificationServiceHelper.getCommentsToMeNotificationId(
           BeeboApplication.getInstance().getAccountBean()));
 }
  @Deprecated
  public static String getTicker(UnreadBean unreadBean) {
    int unreadMentionCmt = unreadBean.getMention_cmt();
    int unreadMentionStatus = unreadBean.getMention_status();
    int unreadCmt = unreadBean.getCmt();

    int messageCount = unreadMentionCmt + unreadMentionStatus + unreadCmt;

    String txt =
        String.format(
            BeeboApplication.getInstance().getString(R.string.new_unread_messages),
            String.valueOf(messageCount));

    return txt;
  }
Exemple #9
0
  public String doPost(String urlAddress, Map<String, String> param) throws WeiboException {
    BeeboApplication globalContext = BeeboApplication.getInstance();
    String errorStr = globalContext.getString(R.string.timeout);
    globalContext = null;
    try {
      URL url = new URL(urlAddress);
      Proxy proxy = getProxy();
      HttpsURLConnection uRLConnection;
      if (proxy != null) {
        uRLConnection = (HttpsURLConnection) url.openConnection(proxy);
      } else {
        uRLConnection = (HttpsURLConnection) url.openConnection();
      }

      uRLConnection.setDoInput(true);
      uRLConnection.setDoOutput(true);
      uRLConnection.setRequestMethod("POST");
      uRLConnection.setUseCaches(false);
      uRLConnection.setConnectTimeout(CONNECT_TIMEOUT);
      uRLConnection.setReadTimeout(READ_TIMEOUT);
      uRLConnection.setInstanceFollowRedirects(false);
      uRLConnection.setRequestProperty("Connection", "Keep-Alive");
      uRLConnection.setRequestProperty("Charset", "UTF-8");
      uRLConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
      uRLConnection.connect();

      DataOutputStream out = new DataOutputStream(uRLConnection.getOutputStream());
      out.write(Utility.encodeUrl(param).getBytes());
      out.flush();
      out.close();
      return handleResponse(uRLConnection);
    } catch (IOException e) {
      e.printStackTrace();
      throw new WeiboException(errorStr, e);
    }
  }
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
   int itemId = item.getItemId();
   if (itemId == android.R.id.home) {
     InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
     if (imm.isActive()) {
       imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
     }
     finish();
   } else if (itemId == R.id.menu_at) {
     Intent intent =
         AtUserActivity.atUserIntent(this, BeeboApplication.getInstance().getAccessTokenHack());
     intent.putExtra(Constants.TOKEN, token);
     startActivityForResult(intent, AT_USER);
   } else if (itemId == R.id.menu_clear) {
     clearContentMenu();
   }
   return true;
 }
  @Deprecated
  public static String getTicker(
      UnreadBean unreadBean,
      MessageListBean mentionsWeibo,
      CommentListBean mentionsComment,
      CommentListBean commentsToMe) {
    int unreadMentionCmt = unreadBean.getMention_cmt();
    int unreadMentionStatus = unreadBean.getMention_status();
    int mention = 0;
    if (SettingUtils.allowMentionToMe() && unreadMentionStatus > 0 && mentionsWeibo != null) {
      int actualFetchedSize = mentionsWeibo.getSize();
      if (actualFetchedSize < Integer.valueOf(SettingUtils.getMsgCount())) {
        mention += actualFetchedSize;
      } else {
        mention += Math.max(actualFetchedSize, unreadMentionStatus);
      }
    }
    if (SettingUtils.allowMentionCommentToMe() && unreadMentionCmt > 0 && mentionsComment != null) {
      int actualFetchedSize = mentionsComment.getSize();
      if (actualFetchedSize < Integer.valueOf(SettingUtils.getMsgCount())) {
        mention += actualFetchedSize;
      } else {
        mention += Math.max(actualFetchedSize, unreadMentionCmt);
      }
    }

    StringBuilder stringBuilder = new StringBuilder();
    if (mention > 0) {
      String txt =
          String.format(
              BeeboApplication.getInstance().getString(R.string.new_mentions),
              String.valueOf(mention));
      stringBuilder.append(txt);
    }

    int unreadCmt = unreadBean.getCmt();

    int cmt = 0;

    if (SettingUtils.allowCommentToMe() && unreadCmt > 0 && commentsToMe != null) {
      //
      int actualFetchedSize = commentsToMe.getSize();
      if (actualFetchedSize < Integer.valueOf(SettingUtils.getMsgCount())) {
        cmt += actualFetchedSize;
      } else {
        cmt += Math.max(actualFetchedSize, unreadCmt);
      }

      if (mention > 0) {
        stringBuilder.append("、");
      }

      if (cmt > 0) {
        String txt =
            String.format(
                BeeboApplication.getInstance().getString(R.string.new_comments),
                String.valueOf(cmt));
        stringBuilder.append(txt);
      }
    }
    return stringBuilder.toString();
  }
 public static void cancel(int id) {
   NotificationManager notificationManager =
       (NotificationManager)
           BeeboApplication.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
   notificationManager.cancel(id);
 }
 public static void show(Notification notification, int id) {
   NotificationManager notificationManager =
       (NotificationManager)
           BeeboApplication.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
   notificationManager.notify(id, notification);
 }
 @Override
 public Loader<CommentTimeLineData> onCreateLoader(int id, Bundle args) {
   getListView().setVisibility(View.INVISIBLE);
   return new CommentsToMeDBLoader(
       getActivity(), BeeboApplication.getInstance().getCurrentAccountId());
 }
Exemple #15
0
  public boolean doUploadFile(
      String urlStr,
      Map<String, String> param,
      String path,
      String imageParamName,
      final FileUploaderHttpHelper.ProgressListener listener)
      throws WeiboException {
    String BOUNDARYSTR = getBoundry();

    File targetFile = new File(path);

    byte[] barry = null;
    int contentLength = 0;
    String sendStr = "";
    try {
      barry = ("--" + BOUNDARYSTR + "--\r\n").getBytes("UTF-8");

      sendStr =
          getBoundaryMessage(
              BOUNDARYSTR, param, imageParamName, new File(path).getName(), "image/png");
      contentLength =
          sendStr.getBytes("UTF-8").length + (int) targetFile.length() + 2 * barry.length;
    } catch (UnsupportedEncodingException e) {

    }
    int totalSent = 0;
    String lenstr = Integer.toString(contentLength);

    HttpURLConnection urlConnection = null;
    BufferedOutputStream out = null;
    FileInputStream fis = null;
    BeeboApplication globalContext = BeeboApplication.getInstance();
    String errorStr = globalContext.getString(R.string.timeout);
    globalContext = null;
    try {
      URL url = null;

      url = new URL(urlStr);

      Proxy proxy = getProxy();
      if (proxy != null) {
        urlConnection = (HttpURLConnection) url.openConnection(proxy);
      } else {
        urlConnection = (HttpURLConnection) url.openConnection();
      }

      urlConnection.setConnectTimeout(UPLOAD_CONNECT_TIMEOUT);
      urlConnection.setReadTimeout(UPLOAD_READ_TIMEOUT);
      urlConnection.setDoInput(true);
      urlConnection.setDoOutput(true);
      urlConnection.setRequestMethod("POST");
      urlConnection.setUseCaches(false);
      urlConnection.setRequestProperty("Connection", "Keep-Alive");
      urlConnection.setRequestProperty("Charset", "UTF-8");
      urlConnection.setRequestProperty(
          "Content-type", "multipart/form-data;boundary=" + BOUNDARYSTR);
      urlConnection.setRequestProperty("Content-Length", lenstr);
      ((HttpURLConnection) urlConnection).setFixedLengthStreamingMode(contentLength);
      urlConnection.connect();

      out = new BufferedOutputStream(urlConnection.getOutputStream());
      out.write(sendStr.getBytes("UTF-8"));
      totalSent += sendStr.getBytes("UTF-8").length;

      fis = new FileInputStream(targetFile);

      int bytesRead;
      int bytesAvailable;
      int bufferSize;
      byte[] buffer;
      int maxBufferSize = 1 * 1024;

      bytesAvailable = fis.available();
      bufferSize = Math.min(bytesAvailable, maxBufferSize);
      buffer = new byte[bufferSize];
      bytesRead = fis.read(buffer, 0, bufferSize);
      long transferred = 0;
      final Thread thread = Thread.currentThread();
      while (bytesRead > 0) {

        if (thread.isInterrupted()) {
          targetFile.delete();
          throw new InterruptedIOException();
        }
        out.write(buffer, 0, bufferSize);
        bytesAvailable = fis.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        bytesRead = fis.read(buffer, 0, bufferSize);
        transferred += bytesRead;
        if (transferred % 50 == 0) {
          out.flush();
        }
        if (listener != null) {
          listener.transferred(transferred);
        }
      }

      out.write(barry);
      totalSent += barry.length;
      out.write(barry);
      totalSent += barry.length;
      out.flush();
      out.close();
      if (listener != null) {
        listener.waitServerResponse();
      }
      int status = urlConnection.getResponseCode();

      if (status != HttpURLConnection.HTTP_OK) {
        String error = handleError(urlConnection);
        throw new WeiboException(error);
      }

      targetFile.delete();

    } catch (IOException e) {
      e.printStackTrace();
      throw new WeiboException(errorStr, e);
    } finally {
      Utility.closeSilently(fis);
      Utility.closeSilently(out);
      if (urlConnection != null) {
        urlConnection.disconnect();
      }
    }

    return true;
  }