コード例 #1
0
ファイル: UnreadWidgetProvider.java プロジェクト: erito/k-9
  public static void updateWidget(
      Context context, AppWidgetManager appWidgetManager, int appWidgetId, String accountUuid) {

    RemoteViews remoteViews =
        new RemoteViews(context.getPackageName(), R.layout.unread_widget_layout);

    int unreadCount = 0;
    String accountName = context.getString(R.string.app_name);
    Intent clickIntent = null;
    try {
      BaseAccount account = null;
      AccountStats stats = null;

      SearchAccount searchAccount = null;
      if (SearchAccount.UNIFIED_INBOX.equals(accountUuid)) {
        searchAccount = SearchAccount.createUnifiedInboxAccount(context);
      } else if (SearchAccount.ALL_MESSAGES.equals(accountUuid)) {
        searchAccount = SearchAccount.createAllMessagesAccount(context);
      }

      if (searchAccount != null) {
        account = searchAccount;
        MessagingController controller = MessagingController.getInstance(K9.app);
        stats = controller.getSearchAccountStatsSynchronous(searchAccount, null);
        clickIntent =
            MessageList.intentDisplaySearch(
                context, searchAccount.getRelatedSearch(), false, true, true);
      } else {
        Account realAccount = Preferences.getPreferences(context).getAccount(accountUuid);
        if (realAccount != null) {
          account = realAccount;
          stats = realAccount.getStats(context);

          if (K9.FOLDER_NONE.equals(realAccount.getAutoExpandFolderName())) {
            clickIntent = FolderList.actionHandleAccountIntent(context, realAccount, null, false);
          } else {
            LocalSearch search = new LocalSearch(realAccount.getAutoExpandFolderName());
            search.addAllowedFolder(realAccount.getAutoExpandFolderName());
            search.addAccountUuid(account.getUuid());
            clickIntent = MessageList.intentDisplaySearch(context, search, false, true, true);
          }
          clickIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        }
      }

      if (account != null) {
        accountName = account.getDescription();
      }

      if (stats != null) {
        unreadCount = stats.unreadMessageCount;
      }
    } catch (Exception e) {
      if (K9.DEBUG) {
        Log.e(K9.LOG_TAG, "Error getting widget configuration", e);
      }
    }

    if (unreadCount <= 0) {
      // Hide TextView for unread count if there are no unread messages.
      remoteViews.setViewVisibility(R.id.unread_count, View.GONE);
    } else {
      remoteViews.setViewVisibility(R.id.unread_count, View.VISIBLE);

      String displayCount =
          (unreadCount <= MAX_COUNT)
              ? String.valueOf(unreadCount)
              : String.valueOf(MAX_COUNT) + "+";
      remoteViews.setTextViewText(R.id.unread_count, displayCount);
    }

    remoteViews.setTextViewText(R.id.account_name, accountName);

    if (clickIntent == null) {
      // If the widget configuration couldn't be loaded we open the configuration
      // activity when the user clicks the widget.
      clickIntent = new Intent(context, UnreadWidgetConfiguration.class);
      clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    }
    clickIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, clickIntent, 0);

    remoteViews.setOnClickPendingIntent(R.id.unread_widget_layout, pendingIntent);

    appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
  }
コード例 #2
0
ファイル: MessageList.java プロジェクト: frankoid/k-9
  private void decodeExtras(Intent intent) {
    String action = intent.getAction();
    if (Intent.ACTION_VIEW.equals(action)) {
      Uri uri = intent.getData();
      List<String> segmentList = uri.getPathSegments();

      String accountId = segmentList.get(0);
      Collection<Account> accounts = Preferences.getPreferences(this).getAvailableAccounts();
      for (Account account : accounts) {
        if (String.valueOf(account.getAccountNumber()).equals(accountId)) {
          mMessageReference = new MessageReference();
          mMessageReference.accountUuid = account.getUuid();
          mMessageReference.folderName = segmentList.get(1);
          mMessageReference.uid = segmentList.get(2);
          break;
        }
      }
    } else if (ACTION_SHORTCUT.equals(action)) {
      // Handle shortcut intents
      String specialFolder = intent.getStringExtra(EXTRA_SPECIAL_FOLDER);
      if (SearchAccount.UNIFIED_INBOX.equals(specialFolder)) {
        mSearch = SearchAccount.createUnifiedInboxAccount(this).getRelatedSearch();
      } else if (SearchAccount.ALL_MESSAGES.equals(specialFolder)) {
        mSearch = SearchAccount.createAllMessagesAccount(this).getRelatedSearch();
      }
    } else if (intent.getStringExtra(SearchManager.QUERY) != null) {
      // check if this intent comes from the system search ( remote )
      if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        // Query was received from Search Dialog
        String query = intent.getStringExtra(SearchManager.QUERY);

        mSearch = new LocalSearch(getString(R.string.search_results));
        mSearch.setManualSearch(true);
        mNoThreading = true;

        mSearch.or(new SearchCondition(Searchfield.SENDER, Attribute.CONTAINS, query));
        mSearch.or(new SearchCondition(Searchfield.SUBJECT, Attribute.CONTAINS, query));
        mSearch.or(new SearchCondition(Searchfield.MESSAGE_CONTENTS, Attribute.CONTAINS, query));

        Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
        if (appData != null) {
          mSearch.addAccountUuid(appData.getString(EXTRA_SEARCH_ACCOUNT));
          // searches started from a folder list activity will provide an account, but no folder
          if (appData.getString(EXTRA_SEARCH_FOLDER) != null) {
            mSearch.addAllowedFolder(appData.getString(EXTRA_SEARCH_FOLDER));
          }
        } else {
          mSearch.addAccountUuid(LocalSearch.ALL_ACCOUNTS);
        }
      }
    } else {
      // regular LocalSearch object was passed
      mSearch = intent.getParcelableExtra(EXTRA_SEARCH);
      mNoThreading = intent.getBooleanExtra(EXTRA_NO_THREADING, false);
    }

    if (mMessageReference == null) {
      mMessageReference = intent.getParcelableExtra(EXTRA_MESSAGE_REFERENCE);
    }

    if (mMessageReference != null) {
      mSearch = new LocalSearch();
      mSearch.addAccountUuid(mMessageReference.accountUuid);
      mSearch.addAllowedFolder(mMessageReference.folderName);
    }

    String[] accountUuids = mSearch.getAccountUuids();
    mSingleAccountMode = (accountUuids.length == 1 && !mSearch.searchAllAccounts());
    mSingleFolderMode = mSingleAccountMode && (mSearch.getFolderNames().size() == 1);

    if (mSingleAccountMode) {
      Preferences prefs = Preferences.getPreferences(getApplicationContext());
      mAccount = prefs.getAccount(accountUuids[0]);

      if (mAccount != null && !mAccount.isAvailable(this)) {
        Log.i(K9.LOG_TAG, "not opening MessageList of unavailable account");
        onAccountUnavailable();
        return;
      }
    }

    if (mSingleFolderMode) {
      mFolderName = mSearch.getFolderNames().get(0);
    }

    // now we know if we are in single account mode and need a subtitle
    mActionBarSubTitle.setVisibility((!mSingleFolderMode) ? View.GONE : View.VISIBLE);
  }