Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 763d3f08 authored by Hari's avatar Hari Committed by cketti
Browse files

Move to ChooseFolder after selecting Account

parent d26483e1
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -144,6 +144,13 @@
            android:configChanges="locale"
            android:label="@string/account_setup_names_title"/>

        <activity
            android:name=".activity.ChooseAccount"
            android:configChanges="locale"
            android:label="@string/choose_account_title"
            android:noHistory="true"
            android:theme="@style/Theme.K9Dialog"/>

        <activity
            android:name=".activity.ChooseFolder"
            android:configChanges="locale"
+44 −0
Original line number Diff line number Diff line
@@ -821,6 +821,50 @@ public class Account implements BaseAccount, StoreConfig {
        return stats;
    }

    public int getFolderUnreadCount(Context context, String folderName) throws MessagingException {
        if (!isAvailable(context)) {
            return 0;
        }

        int unreadMessageCount = 0;

        Cursor cursor = loadUnreadCountForFolder(context, folderName);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                unreadMessageCount = cursor.getInt(0);
            }
        } finally {
            Utility.closeQuietly(cursor);
        }

        return unreadMessageCount;
    }

    private Cursor loadUnreadCountForFolder(Context context, String folderName) {
        ContentResolver cr = context.getContentResolver();

        Uri uri = Uri.withAppendedPath(EmailProvider.CONTENT_URI,
                "account/" + getUuid() + "/stats");

        String[] projection = {
                StatsColumns.UNREAD_COUNT,
        };

        LocalSearch search = new LocalSearch();
        search.addAllowedFolder(folderName);

        // Use the LocalSearch instance to create a WHERE clause to query the content provider
        StringBuilder query = new StringBuilder();
        List<String> queryArgs = new ArrayList<>();
        ConditionsTreeNode conditions = search.getConditions();
        SqlQueryBuilder.buildWhereClause(this, conditions, query, queryArgs);

        String selection = query.toString();
        String[] selectionArgs = queryArgs.toArray(new String[queryArgs.size()]);

        return cr.query(uri, projection, selection, selectionArgs, null);
    }


    public synchronized void setChipColor(int color) {
        chipColor = color;
+23 −0
Original line number Diff line number Diff line
package com.fsck.k9.activity;

import android.content.Intent;

import com.fsck.k9.BaseAccount;

public class ChooseAccount extends AccountList {

    public static final String EXTRA_ACCOUNT_UUID = "com.fsck.k9.ChooseAccount_account_uuid";

    @Override
    protected boolean displaySpecialAccounts() {
        return true;
    }

    @Override
    protected void onAccountSelected(BaseAccount account) {
        Intent intent = new Intent();
        intent.putExtra(EXTRA_ACCOUNT_UUID, account.getUuid());
        setResult(RESULT_OK, intent);
        finish();
    }
}
+158 −20
Original line number Diff line number Diff line
@@ -6,32 +6,55 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;

import com.fsck.k9.BaseAccount;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.fsck.k9.Account;
import com.fsck.k9.Preferences;
import com.fsck.k9.R;
import com.fsck.k9.helper.UnreadWidgetProperties;
import com.fsck.k9.provider.UnreadWidgetProvider;
import com.fsck.k9.search.SearchAccount;

import timber.log.Timber;


/**
 * Activity to select an account for the unread widget.
 */
public class UnreadWidgetConfiguration extends AccountList {
public class UnreadWidgetConfiguration extends K9PreferenceActivity {
    /**
     * Name of the preference file to store the widget configuration.
     */
    private static final String PREFS_NAME = "unread_widget_configuration.xml";

    /**
     * Prefix for the preference keys.
     * Prefixes for the preference keys
     */
    private static final String PREF_PREFIX_KEY = "unread_widget.";
    private static final String PREF_FOLDER_NAME_SUFFIX_KEY = ".folder_name";

    private static final String PREFERENCE_UNREAD_ACCOUNT = "unread_account";
    private static final String PREFERENCE_UNREAD_FOLDER_ENABLED = "unread_folder_enabled";
    private static final String PREFERENCE_UNREAD_FOLDER = "unread_folder";

    /**
     * The ID of the widget we are configuring.
     */
    private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
    private int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

    private static final int REQUEST_CHOOSE_ACCOUNT = 1;
    private static final int REQUEST_CHOOSE_FOLDER = 2;

    private Preference unreadAccount;
    private CheckBoxPreference unreadFolderEnabled;
    private Preference unreadFolder;

    private String selectedAccountUuid;
    private String selectedFolderName;

    @Override
    public void onCreate(Bundle icicle) {
@@ -41,58 +64,173 @@ public class UnreadWidgetConfiguration extends AccountList {
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
            appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        // If they gave us an intent without the widget ID, just bail.
        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
        if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
            Timber.e("Received an invalid widget ID");
            finish();
            return;
        }

        addPreferencesFromResource(R.xml.unread_widget_configuration);
        unreadAccount = findPreference(PREFERENCE_UNREAD_ACCOUNT);
        unreadAccount.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Intent intent = new Intent(UnreadWidgetConfiguration.this, ChooseAccount.class);
                startActivityForResult(intent, REQUEST_CHOOSE_ACCOUNT);
                return false;
            }
        });

        unreadFolderEnabled = (CheckBoxPreference) findPreference(PREFERENCE_UNREAD_FOLDER_ENABLED);
        unreadFolderEnabled.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                unreadFolder.setSummary(getString(R.string.unread_widget_folder_summary));
                selectedFolderName = null;
                return true;
            }
        });

        unreadFolder = findPreference(PREFERENCE_UNREAD_FOLDER);
        unreadFolder.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Intent intent = new Intent(UnreadWidgetConfiguration.this, ChooseFolder.class);
                intent.putExtra(ChooseFolder.EXTRA_ACCOUNT, selectedAccountUuid);
                intent.putExtra(ChooseFolder.EXTRA_SHOW_DISPLAYABLE_ONLY, "yes");
                startActivityForResult(intent, REQUEST_CHOOSE_FOLDER);
                return false;
            }
        });
        setTitle(R.string.unread_widget_select_account);
    }

    @Override
    protected boolean displaySpecialAccounts() {
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_CHOOSE_ACCOUNT:
                    handleChooseAccount(data.getStringExtra(ChooseAccount.EXTRA_ACCOUNT_UUID));
                    break;
                case REQUEST_CHOOSE_FOLDER:
                    handleChooseFolder(data.getStringExtra(ChooseFolder.EXTRA_NEW_FOLDER));
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void handleChooseAccount(String accountUuid) {
        selectedAccountUuid = accountUuid;
        unreadFolder.setSummary(getString(R.string.unread_widget_folder_summary));
        if (SearchAccount.UNIFIED_INBOX.equals(selectedAccountUuid) ||
                SearchAccount.ALL_MESSAGES.equals(selectedAccountUuid)) {
            handleSearchAccount();
        } else {
            handleRegularAccount();
        }
    }

    private void handleSearchAccount() {
        if (SearchAccount.UNIFIED_INBOX.equals(selectedAccountUuid)) {
            unreadAccount.setSummary(R.string.unread_widget_unified_inbox_account_summary);
        } else if (SearchAccount.ALL_MESSAGES.equals(selectedAccountUuid)) {
            unreadAccount.setSummary(R.string.unread_widget_all_messages_account_summary);
        }
        unreadFolderEnabled.setEnabled(false);
        unreadFolderEnabled.setChecked(false);
        unreadFolder.setEnabled(false);
        selectedFolderName = null;
    }

    private void handleRegularAccount() {
        Account selectedAccount = Preferences.getPreferences(this).getAccount(selectedAccountUuid);
        String summary = selectedAccount.getDescription();
        if (summary == null || summary.isEmpty()) {
            summary = selectedAccount.getEmail();
        }
        unreadAccount.setSummary(summary);
        unreadFolderEnabled.setEnabled(true);
        unreadFolder.setEnabled(true);
    }

    private void handleChooseFolder(String folderName) {
        selectedFolderName = folderName;
        unreadFolder.setSummary(selectedFolderName);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.unread_widget_option, menu);
        return true;
    }

    @Override
    protected void onAccountSelected(BaseAccount account) {
        // Save widget configuration
        String accountUuid = account.getUuid();
        saveAccountUuid(this, mAppWidgetId, accountUuid);
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.done:
                if (validateWidget()) {
                    updateWidgetAndExit();
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private boolean validateWidget() {
        if (selectedAccountUuid == null) {
            Toast.makeText(this, R.string.unread_widget_account_not_selected, Toast.LENGTH_LONG).show();
            return false;
        } else if (unreadFolderEnabled.isChecked() && selectedFolderName == null) {
            Toast.makeText(this, R.string.unread_widget_folder_not_selected, Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }

    private void updateWidgetAndExit() {
        UnreadWidgetProperties properties = new UnreadWidgetProperties(appWidgetId, selectedAccountUuid,selectedFolderName);
        saveWidgetProperties(this, properties);

        // Update widget
        Context context = getApplicationContext();
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        UnreadWidgetProvider.updateWidget(context, appWidgetManager, mAppWidgetId, accountUuid);
        UnreadWidgetProvider.updateWidget(context, appWidgetManager, properties);

        // Let the caller know that the configuration was successful
        Intent resultValue = new Intent();
        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        setResult(RESULT_OK, resultValue);
        finish();
    }

    private static void saveAccountUuid(Context context, int appWidgetId, String accountUuid) {
    private static void saveWidgetProperties(Context context, UnreadWidgetProperties properties) {
        int appWidgetId = properties.getAppWidgetId();
        SharedPreferences.Editor editor =
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit();
        editor.putString(PREF_PREFIX_KEY + appWidgetId, accountUuid);
        editor.commit();
        editor.putString(PREF_PREFIX_KEY + appWidgetId, properties.getAccountUuid());
        editor.putString(PREF_PREFIX_KEY + appWidgetId + PREF_FOLDER_NAME_SUFFIX_KEY, properties.getFolderName());
        editor.apply();
    }

    public static String getAccountUuid(Context context, int appWidgetId) {
    public static UnreadWidgetProperties getWidgetProperties(Context context, int appWidgetId) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        String accountUuid = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);
        return accountUuid;
        String folderName = prefs.getString(PREF_PREFIX_KEY + appWidgetId + PREF_FOLDER_NAME_SUFFIX_KEY, null);
        return new UnreadWidgetProperties(appWidgetId, accountUuid, folderName);
    }

    public static void deleteWidgetConfiguration(Context context, int appWidgetId) {
        Editor editor = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit();
        editor.remove(PREF_PREFIX_KEY + appWidgetId);
        editor.commit();
        editor.remove(PREF_PREFIX_KEY + appWidgetId + PREF_FOLDER_NAME_SUFFIX_KEY);
        editor.apply();
    }
}
+138 −0
Original line number Diff line number Diff line
package com.fsck.k9.helper;

import android.content.Context;
import android.content.Intent;

import com.fsck.k9.Account;
import com.fsck.k9.AccountStats;
import com.fsck.k9.BaseAccount;
import com.fsck.k9.K9;
import com.fsck.k9.Preferences;
import com.fsck.k9.R;
import com.fsck.k9.activity.FolderList;
import com.fsck.k9.activity.MessageList;
import com.fsck.k9.controller.MessagingController;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.search.LocalSearch;
import com.fsck.k9.search.SearchAccount;

public class UnreadWidgetProperties {

    private int appWidgetId;
    private String accountUuid;
    private String folderName;
    private Type type;

    public UnreadWidgetProperties(int appWidgetId, String accountUuid, String folderName) {
        this.appWidgetId = appWidgetId;
        this.accountUuid = accountUuid;
        this.folderName = folderName;
        calculateType();
    }

    public String getTitle(Context context) {
        String accountName = getAccount(context).getDescription();
        switch (type) {
            case SEARCH_ACCOUNT:
            case ACCOUNT:
                return accountName;
            case FOLDER:
                return context.getString(R.string.unread_widget_title, accountName, folderName);
            default:
                return null;
        }
    }

    public int getUnreadCount(Context context) throws MessagingException {
        BaseAccount baseAccount = getAccount(context);
        AccountStats stats;
        switch (type) {
            case SEARCH_ACCOUNT:
                MessagingController controller = MessagingController.getInstance(context);
                stats = controller.getSearchAccountStatsSynchronous((SearchAccount) baseAccount, null);
                return stats.unreadMessageCount;
            case ACCOUNT:
                Account account = (Account) baseAccount;
                stats = account.getStats(context);
                return stats.unreadMessageCount;
            case FOLDER:
                return ((Account) baseAccount).getFolderUnreadCount(context, folderName);
            default:
                return -1;
        }
    }

    public Intent getClickIntent(Context context) {
        switch (type) {
            case SEARCH_ACCOUNT:
                SearchAccount searchAccount = (SearchAccount) getAccount(context);
                return MessageList.intentDisplaySearch(context,
                        searchAccount.getRelatedSearch(), false, true, true);
            case ACCOUNT:
                return getClickIntentForAccount(context);
            case FOLDER:
                return getClickIntentForFolder(context);
            default:
                return null;
        }
    }

    public int getAppWidgetId() {
        return appWidgetId;
    }

    public String getAccountUuid() {
        return accountUuid;
    }

    public String getFolderName() {
        return folderName;
    }

    private void calculateType() {
        if (SearchAccount.UNIFIED_INBOX.equals(accountUuid) ||
                SearchAccount.ALL_MESSAGES.equals(accountUuid)) {
            type = Type.SEARCH_ACCOUNT;
        } else if(folderName != null) {
            type = Type.FOLDER;
        } else {
            type = Type.ACCOUNT;
        }
    }

    private BaseAccount getAccount(Context context) {
        if (SearchAccount.UNIFIED_INBOX.equals(accountUuid)) {
            return SearchAccount.createUnifiedInboxAccount(context);
        } else if (SearchAccount.ALL_MESSAGES.equals(accountUuid)) {
            return SearchAccount.createAllMessagesAccount(context);
        }
        return Preferences.getPreferences(context).getAccount(accountUuid);
    }

    private Intent getClickIntentForAccount(Context context) {
        Account account = Preferences.getPreferences(context).getAccount(accountUuid);
        if (K9.FOLDER_NONE.equals(account.getAutoExpandFolderName())) {
            return FolderList.actionHandleAccountIntent(context, account, false);
        }
        LocalSearch search = new LocalSearch(account.getAutoExpandFolderName());
        search.addAllowedFolder(account.getAutoExpandFolderName());
        search.addAccountUuid(account.getUuid());
        return MessageList.intentDisplaySearch(context, search, false, true, true);
    }

    private Intent getClickIntentForFolder(Context context) {
        Account account = Preferences.getPreferences(context).getAccount(accountUuid);
        LocalSearch search = new LocalSearch(folderName);
        search.addAllowedFolder(folderName);
        search.addAccountUuid(account.getUuid());
        Intent clickIntent = MessageList.intentDisplaySearch(context, search, false, true, true);
        clickIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        return clickIntent;
    }

    public enum Type {
        SEARCH_ACCOUNT,
        ACCOUNT,
        FOLDER
    }
}
Loading