public boolean isConnectionStateOk(ConnectionState connectionState) {
   switch (this) {
     case ANY:
       return true;
     default:
       switch (connectionState) {
         case ONLINE:
           switch (this) {
             case SYNC:
               return !MyPreferences.isSyncOverWiFiOnly();
             case DOWNLOAD_ATTACHMENT:
               return !MyPreferences.isSyncOverWiFiOnly()
                   && !MyPreferences.isDownloadAttachmentsOverWiFiOnly();
             case OFFLINE:
               return false;
             default:
               return true;
           }
         case WIFI:
           return (this != ConnectionRequired.OFFLINE);
         case OFFLINE:
           return (this == ConnectionRequired.OFFLINE);
         default:
           return true;
       }
   }
 }
 private boolean areNotificationsEnabled(TimelineType timelineType) {
   switch (timelineType) {
     case MENTIONS:
       return MyPreferences.getBoolean(MyPreferences.KEY_NOTIFY_OF_MENTIONS, false);
     case DIRECT:
       return MyPreferences.getBoolean(MyPreferences.KEY_NOTIFY_OF_DIRECT_MESSAGES, false);
     case HOME:
       return MyPreferences.getBoolean(MyPreferences.KEY_NOTIFY_OF_HOME_TIMELINE, false);
     default:
       return true;
   }
 }
 private void setPreferences() {
   MyPreferences.getDefaultSharedPreferences()
       .edit()
       .putBoolean(MyPreferences.KEY_SHOW_ATTACHED_IMAGES, showAttachedImages)
       .putBoolean(MyPreferences.KEY_SHOW_AVATARS, showAvatars)
       .commit();
 }
  @Override
  protected void setUp() throws Exception {
    super.setUp();
    TestSuite.initializeWithData(this);
    iteration = (iteration >= 4 ? 1 : iteration + 1);
    switch (iteration) {
      case 2:
        showAttachedImages = showAttachedImagesOld;
        showAvatars = !showAvatarsOld;
        break;
      case 3:
        showAttachedImages = !showAttachedImagesOld;
        showAvatars = showAvatarsOld;
        break;
      case 4:
        showAttachedImages = !showAttachedImagesOld;
        showAvatars = !showAvatarsOld;
        break;
      default:
        showAttachedImagesOld = MyPreferences.showAttachedImages();
        showAvatarsOld = MyPreferences.showAvatars();
        showAttachedImages = showAttachedImagesOld;
        showAvatars = showAvatarsOld;
        break;
    }
    setPreferences();
    MyLog.setLogToFile(true);
    logStartStop("setUp started");

    MyAccount ma =
        MyContextHolder.get()
            .persistentAccounts()
            .fromAccountName(TestSuite.CONVERSATION_ACCOUNT_NAME);
    assertTrue(ma.isValid());
    MyContextHolder.get().persistentAccounts().setCurrentAccount(ma);

    Intent intent =
        new Intent(
            Intent.ACTION_VIEW,
            MatchedUri.getTimelineUri(ma.getUserId(), TimelineType.HOME, false, 0));
    setActivityIntent(intent);

    activity = getActivity();

    assertTrue("MyService is available", MyServiceManager.isServiceAvailable());
    logStartStop("setUp ended");
  }
 public void update(CommandResult result) {
   if (!MyPreferences.getBoolean(MyPreferences.KEY_NOTIFICATIONS_ENABLED, false)) {
     return;
   }
   notifyForOneType(TimelineType.HOME, result.getMessagesAdded());
   notifyForOneType(TimelineType.MENTIONS, result.getMentionsAdded());
   notifyForOneType(TimelineType.DIRECT, result.getDirectedAdded());
 }
示例#6
0
 @NonNull
 public static Drawable getDrawable(long authorId, Cursor cursor) {
   Drawable drawable = null;
   if (MyPreferences.getShowAvatars()) {
     String avatarFilename = DbUtils.getString(cursor, DownloadTable.AVATAR_FILE_NAME);
     AvatarFile avatarFile = new AvatarFile(authorId, avatarFilename);
     drawable = avatarFile.getDrawable();
   }
   if (drawable == null) {
     drawable = getDefaultDrawable();
   }
   return drawable;
 }
  private void notify(TimelineType timelineType, int messageTitleResId, String messageText) {
    String ringtone = MyPreferences.getString(MyPreferences.KEY_RINGTONE_PREFERENCE, null);
    Uri sound = TextUtils.isEmpty(ringtone) ? null : Uri.parse(ringtone);

    Notification.Builder builder =
        new Notification.Builder(myContext.context())
            .setSmallIcon(
                MyPreferences.getBoolean(MyPreferences.KEY_NOTIFICATION_ICON_ALTERNATIVE, false)
                    ? R.drawable.notification_icon_circle
                    : R.drawable.notification_icon)
            .setContentTitle(myContext.context().getText(messageTitleResId))
            .setContentText(messageText)
            .setSound(sound);

    if (mNotificationsVibrate) {
      builder.setVibrate(new long[] {200, 300, 200, 300});
    }
    builder.setLights(Color.GREEN, 500, 1000);

    // Prepare "intent" to launch timeline activities exactly like in
    // org.andstatus.app.TimelineActivity.onOptionsItemSelected
    Intent intent = new Intent(myContext.context(), TimelineActivity.class);
    intent.setData(
        Uri.withAppendedPath(
            MatchedUri.getTimelineUri(
                0, timelineType, myContext.persistentAccounts().size() > 1, 0),
            "rnd/" + android.os.SystemClock.elapsedRealtime()));
    PendingIntent pendingIntent =
        PendingIntent.getActivity(
            myContext.context(),
            timelineType.hashCode(),
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pendingIntent);
    myContext.notify(timelineType, builder.build());
  }
示例#8
0
 private void optionallyLoadAvatar(long userId, ContentValues values) {
   if (MyPreferences.showAvatars() && values.containsKey(User.AVATAR_URL)) {
     AvatarData.getForUser(userId).requestDownload();
   }
 }
 private void setMessageAuthor(Cursor cursor, int columnIndex, TextView view) {
   view.setText(
       TimelineSql.userColumnIndexToNameAtTimeline(
           cursor, columnIndex, MyPreferences.showOrigin()));
 }
 private AddedMessagesNotifier(MyContext myContext) {
   this.myContext = myContext;
   mNotificationsVibrate = MyPreferences.getBoolean("vibration", false);
 }
  /** Formats message as a View suitable for a conversation list */
  private View oneMessageToView(ConversationOneMessage oMsg) {
    final String method = "oneMessageToView";
    if (MyLog.isLoggable(this, MyLog.VERBOSE)) {
      MyLog.v(
          this,
          method
              + ": msgId="
              + oMsg.msgId
              + (oMsg.avatarDrawable != null
                  ? ", avatar=" + oMsg.avatarDrawable.getFileName()
                  : ""));
    }
    LayoutInflater inflater = LayoutInflater.from(context);
    int layoutResource = R.layout.message_conversation;
    if (!Activity.class.isAssignableFrom(context.getClass())) {
      MyLog.w(this, "Context should be from an Activity");
    }
    View messageView = inflater.inflate(layoutResource, null);
    messageView.setOnCreateContextMenuListener(contextMenu);

    float displayDensity = context.getResources().getDisplayMetrics().density;
    // See
    // http://stackoverflow.com/questions/2238883/what-is-the-correct-way-to-specify-dimensions-in-dip-from-java-code
    int indent0 = (int) (10 * displayDensity);
    int indentPixels = indent0 * oMsg.indentLevel;

    LinearLayout messageIndented = (LinearLayout) messageView.findViewById(R.id.message_indented);
    if (oMsg.msgId == selectedMessageId && oMsgs.size() > 1) {
      messageIndented.setBackgroundDrawable(
          context.getResources().getDrawable(R.drawable.message_current_background));
    }

    int viewToTheLeftId = 0;
    if (oMsg.indentLevel > 0) {
      View divider = messageView.findViewById(R.id.divider);
      RelativeLayout.LayoutParams layoutParams =
          new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);
      layoutParams.leftMargin = indentPixels - 4;
      divider.setLayoutParams(layoutParams);

      if (MyLog.isLoggable(this, MyLog.VERBOSE)) {
        MyLog.v(this, "density=" + displayDensity);
      }
      viewToTheLeftId = 2;
      ImageView indentView =
          new ConversationIndentImageView(context, messageIndented, indentPixels);
      indentView.setId(viewToTheLeftId);
      ((ViewGroup) messageIndented.getParent()).addView(indentView);
    }

    if (MyPreferences.showAvatars()) {
      ImageView avatarView = new ImageView(context);
      int size = Math.round(AvatarDrawable.AVATAR_SIZE_DIP * displayDensity);
      avatarView.setScaleType(ScaleType.FIT_CENTER);
      RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(size, size);
      layoutParams.topMargin = 3;
      if (oMsg.indentLevel > 0) {
        layoutParams.leftMargin = 1;
      }
      if (viewToTheLeftId == 0) {
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
      } else {
        layoutParams.addRule(RelativeLayout.RIGHT_OF, viewToTheLeftId);
      }
      avatarView.setLayoutParams(layoutParams);
      avatarView.setImageDrawable(oMsg.avatarDrawable.getDrawable());
      indentPixels += size;
      ((ViewGroup) messageIndented.getParent()).addView(avatarView);
    }
    messageIndented.setPadding(indentPixels + 6, 2, 6, 2);

    TextView id = (TextView) messageView.findViewById(R.id.id);
    id.setText(Long.toString(oMsg.msgId));
    TextView linkedUserId = (TextView) messageView.findViewById(R.id.linked_user_id);
    linkedUserId.setText(Long.toString(oMsg.linkedUserId));

    TextView author = (TextView) messageView.findViewById(R.id.message_author);
    TextView body = (TextView) messageView.findViewById(R.id.message_body);
    TextView details = (TextView) messageView.findViewById(R.id.message_details);

    author.setText(oMsg.author);

    TextView number = (TextView) messageView.findViewById(R.id.message_number);
    number.setText(Integer.toString(oMsg.historyOrder));

    if (!TextUtils.isEmpty(oMsg.body)) {
      body.setLinksClickable(true);
      body.setMovementMethod(LinkMovementMethod.getInstance());
      body.setFocusable(true);
      body.setFocusableInTouchMode(true);
      Spanned spanned = Html.fromHtml(oMsg.body);
      body.setText(spanned);
      if (!MbMessage.hasUrlSpans(spanned)) {
        Linkify.addLinks(body, Linkify.ALL);
      }
    }

    // Everything else goes to messageDetails
    String messageDetails = RelativeTime.getDifference(context, oMsg.createdDate);
    if (!SharedPreferencesUtil.isEmpty(oMsg.via)) {
      messageDetails +=
          " "
              + String.format(
                  MyContextHolder.get().getLocale(),
                  context.getText(R.string.message_source_from).toString(),
                  oMsg.via);
    }
    if (oMsg.inReplyToMsgId != 0) {
      String inReplyToName = oMsg.inReplyToName;
      if (SharedPreferencesUtil.isEmpty(inReplyToName)) {
        inReplyToName = "...";
      }
      messageDetails +=
          " "
              + String.format(
                  MyContextHolder.get().getLocale(),
                  context.getText(R.string.message_source_in_reply_to).toString(),
                  oMsg.inReplyToName)
              + " ("
              + msgIdToHistoryOrder(oMsg.inReplyToMsgId)
              + ")";
    }
    if (!SharedPreferencesUtil.isEmpty(oMsg.rebloggersString)
        && !oMsg.rebloggersString.equals(oMsg.author)) {
      if (!SharedPreferencesUtil.isEmpty(oMsg.inReplyToName)) {
        messageDetails += ";";
      }
      messageDetails +=
          " "
              + String.format(
                  MyContextHolder.get().getLocale(),
                  context
                      .getText(ma.alternativeTermForResourceId(R.string.reblogged_by))
                      .toString(),
                  oMsg.rebloggersString);
    }
    if (!SharedPreferencesUtil.isEmpty(oMsg.recipientName)) {
      messageDetails +=
          " "
              + String.format(
                  MyContextHolder.get().getLocale(),
                  context.getText(R.string.message_source_to).toString(),
                  oMsg.recipientName);
    }
    if (MyLog.isLoggable(this, MyLog.VERBOSE)) {
      messageDetails = messageDetails + " (i" + oMsg.indentLevel + ",r" + oMsg.replyLevel + ")";
    }
    details.setText(messageDetails);
    ImageView favorited = (ImageView) messageView.findViewById(R.id.message_favorited);
    favorited.setImageResource(
        oMsg.favorited ? android.R.drawable.star_on : android.R.drawable.star_off);
    return messageView;
  }
  private void loadMessageFromCursor(ConversationOneMessage oMsg, Cursor cursor) {
    /**
     * IDs of all known senders of this message except for the Author These "senders" reblogged the
     * message
     */
    Set<Long> rebloggers = new HashSet<Long>();
    int ind = 0;
    do {
      long senderId = cursor.getLong(cursor.getColumnIndex(Msg.SENDER_ID));
      long authorId = cursor.getLong(cursor.getColumnIndex(Msg.AUTHOR_ID));
      long linkedUserId = cursor.getLong(cursor.getColumnIndex(User.LINKED_USER_ID));

      if (ind == 0) {
        // This is the same for all retrieved rows
        oMsg.inReplyToMsgId = cursor.getLong(cursor.getColumnIndex(Msg.IN_REPLY_TO_MSG_ID));
        oMsg.createdDate = cursor.getLong(cursor.getColumnIndex(Msg.CREATED_DATE));
        oMsg.author = cursor.getString(cursor.getColumnIndex(User.AUTHOR_NAME));
        oMsg.body = cursor.getString(cursor.getColumnIndex(Msg.BODY));
        String via = cursor.getString(cursor.getColumnIndex(Msg.VIA));
        if (!TextUtils.isEmpty(via)) {
          oMsg.via = Html.fromHtml(via).toString().trim();
        }
        if (MyPreferences.showAvatars()) {
          oMsg.avatarDrawable =
              new AvatarDrawable(
                  authorId, cursor.getString(cursor.getColumnIndex(Avatar.FILE_NAME)));
        }
        int colIndex = cursor.getColumnIndex(User.IN_REPLY_TO_NAME);
        if (colIndex > -1) {
          oMsg.inReplyToName = cursor.getString(colIndex);
          if (TextUtils.isEmpty(oMsg.inReplyToName)) {
            oMsg.inReplyToName = "";
          }
        }
        colIndex = cursor.getColumnIndex(User.RECIPIENT_NAME);
        if (colIndex > -1) {
          oMsg.recipientName = cursor.getString(colIndex);
          if (TextUtils.isEmpty(oMsg.recipientName)) {
            oMsg.recipientName = "";
          }
        }
      }

      if (senderId != authorId) {
        rebloggers.add(senderId);
      }
      if (linkedUserId != 0) {
        if (oMsg.linkedUserId == 0) {
          oMsg.linkedUserId = linkedUserId;
        }
        if (cursor.getInt(cursor.getColumnIndex(MsgOfUser.REBLOGGED)) == 1
            && linkedUserId != authorId) {
          rebloggers.add(linkedUserId);
        }
        if (cursor.getInt(cursor.getColumnIndex(MsgOfUser.FAVORITED)) == 1) {
          oMsg.favorited = true;
        }
      }

      ind++;
    } while (cursor.moveToNext());

    for (long rebloggerId : rebloggers) {
      if (!TextUtils.isEmpty(oMsg.rebloggersString)) {
        oMsg.rebloggersString += ", ";
      }
      oMsg.rebloggersString += MyProvider.userIdToName(rebloggerId);
    }
  }