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

Commit b4c5df33 authored by stefan-niedermann's avatar stefan-niedermann
Browse files

Fix NotesList Widget

parent 25357d7a
Loading
Loading
Loading
Loading
+13 −8
Original line number Original line Diff line number Diff line
@@ -30,6 +30,7 @@ public class EditNoteActivity extends AppCompatActivity implements BaseNoteFragm
    private static final String INTENT_GOOGLE_ASSISTANT = "com.google.android.gm.action.AUTO_SEND";
    private static final String INTENT_GOOGLE_ASSISTANT = "com.google.android.gm.action.AUTO_SEND";
    private static final String MIMETYPE_TEXT_PLAIN = "text/plain";
    private static final String MIMETYPE_TEXT_PLAIN = "text/plain";
    public static final String PARAM_NOTE_ID = "noteId";
    public static final String PARAM_NOTE_ID = "noteId";
    public static final String PARAM_ACCOUNT_ID = "accountId";
    public static final String PARAM_CATEGORY = "category";
    public static final String PARAM_CATEGORY = "category";


    private BaseNoteFragment fragment;
    private BaseNoteFragment fragment;
@@ -66,6 +67,10 @@ public class EditNoteActivity extends AppCompatActivity implements BaseNoteFragm
        return getIntent().getLongExtra(PARAM_NOTE_ID, 0);
        return getIntent().getLongExtra(PARAM_NOTE_ID, 0);
    }
    }


    private long getAccountId() {
        return getIntent().getLongExtra(PARAM_ACCOUNT_ID, 0);
    }

    /**
    /**
     * Starts the note fragment for an existing note or a new note.
     * Starts the note fragment for an existing note or a new note.
     * The actual behavior is triggered by the activity's intent.
     * The actual behavior is triggered by the activity's intent.
@@ -73,7 +78,7 @@ public class EditNoteActivity extends AppCompatActivity implements BaseNoteFragm
    private void launchNoteFragment() {
    private void launchNoteFragment() {
        long noteId = getNoteId();
        long noteId = getNoteId();
        if (noteId > 0) {
        if (noteId > 0) {
            launchExistingNote(noteId);
            launchExistingNote(getAccountId(), noteId);
        } else {
        } else {
            launchNewNote();
            launchNewNote();
        }
        }
@@ -85,7 +90,7 @@ public class EditNoteActivity extends AppCompatActivity implements BaseNoteFragm
     *
     *
     * @param noteId ID of the existing note.
     * @param noteId ID of the existing note.
     */
     */
    private void launchExistingNote(long noteId) {
    private void launchExistingNote(long accountId, long noteId) {
        final String prefKeyNoteMode = getString(R.string.pref_key_note_mode);
        final String prefKeyNoteMode = getString(R.string.pref_key_note_mode);
        final String prefKeyLastMode = getString(R.string.pref_key_last_note_mode);
        final String prefKeyLastMode = getString(R.string.pref_key_last_note_mode);
        final String prefValueEdit = getString(R.string.pref_value_mode_edit);
        final String prefValueEdit = getString(R.string.pref_value_mode_edit);
@@ -99,7 +104,7 @@ public class EditNoteActivity extends AppCompatActivity implements BaseNoteFragm
        if (prefValuePreview.equals(mode) || (prefValueLast.equals(mode) && prefValuePreview.equals(lastMode))) {
        if (prefValuePreview.equals(mode) || (prefValueLast.equals(mode) && prefValuePreview.equals(lastMode))) {
            editMode = false;
            editMode = false;
        }
        }
        launchExistingNote(noteId, editMode);
        launchExistingNote(accountId, noteId, editMode);
    }
    }


    /**
    /**
@@ -110,16 +115,16 @@ public class EditNoteActivity extends AppCompatActivity implements BaseNoteFragm
     *               <code>true</code> for {@link NoteEditFragment},
     *               <code>true</code> for {@link NoteEditFragment},
     *               <code>false</code> for {@link NotePreviewFragment}.
     *               <code>false</code> for {@link NotePreviewFragment}.
     */
     */
    private void launchExistingNote(long noteId, boolean edit) {
    private void launchExistingNote(long accountId, long noteId, boolean edit) {
        // save state of the fragment in order to resume with the same note and originalNote
        // save state of the fragment in order to resume with the same note and originalNote
        Fragment.SavedState savedState = null;
        Fragment.SavedState savedState = null;
        if (fragment != null) {
        if (fragment != null) {
            savedState = getFragmentManager().saveFragmentInstanceState(fragment);
            savedState = getFragmentManager().saveFragmentInstanceState(fragment);
        }
        }
        if (edit) {
        if (edit) {
            fragment = NoteEditFragment.newInstance(noteId);
            fragment = NoteEditFragment.newInstance(accountId, noteId);
        } else {
        } else {
            fragment = NotePreviewFragment.newInstance(noteId);
            fragment = NotePreviewFragment.newInstance(accountId, noteId);
        }
        }


        if (savedState != null) {
        if (savedState != null) {
@@ -175,10 +180,10 @@ public class EditNoteActivity extends AppCompatActivity implements BaseNoteFragm
                close();
                close();
                return true;
                return true;
            case R.id.menu_preview:
            case R.id.menu_preview:
                launchExistingNote(getNoteId(), false);
                launchExistingNote(getAccountId(), getNoteId(), false);
                return true;
                return true;
            case R.id.menu_edit:
            case R.id.menu_edit:
                launchExistingNote(getNoteId(), true);
                launchExistingNote(getAccountId(), getNoteId(), true);
                return true;
                return true;
            default:
            default:
                return super.onOptionsItemSelected(item);
                return super.onOptionsItemSelected(item);
+18 −10
Original line number Original line Diff line number Diff line
@@ -15,15 +15,19 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView;


import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException;
import com.nextcloud.android.sso.helper.SingleAccountHelper;

import java.util.ArrayList;
import java.util.ArrayList;
import java.util.List;
import java.util.List;
import java.util.Map;
import java.util.Map;


import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.android.activity.NotesListViewActivity;
import it.niedermann.owncloud.notes.android.activity.NotesListViewActivity;
import it.niedermann.owncloud.notes.model.LocalAccount;
import it.niedermann.owncloud.notes.model.NavigationAdapter;
import it.niedermann.owncloud.notes.model.NavigationAdapter;
import it.niedermann.owncloud.notes.persistence.NoteSQLiteOpenHelper;
import it.niedermann.owncloud.notes.persistence.NoteSQLiteOpenHelper;
import it.niedermann.owncloud.notes.persistence.NoteServerSyncHelper;
import it.niedermann.owncloud.notes.util.Notes;
import it.niedermann.owncloud.notes.util.Notes;


public class NoteListWidgetConfiguration extends AppCompatActivity {
public class NoteListWidgetConfiguration extends AppCompatActivity {
@@ -31,6 +35,9 @@ public class NoteListWidgetConfiguration extends AppCompatActivity {


    private int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
    private int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;



    LocalAccount localAccount = null;

    private NavigationAdapter adapterCategories;
    private NavigationAdapter adapterCategories;
    private NavigationAdapter.NavigationItem itemRecent, itemFavorites;
    private NavigationAdapter.NavigationItem itemRecent, itemFavorites;
    private NoteSQLiteOpenHelper db = null;
    private NoteSQLiteOpenHelper db = null;
@@ -41,15 +48,16 @@ public class NoteListWidgetConfiguration extends AppCompatActivity {
        setResult(RESULT_CANCELED);
        setResult(RESULT_CANCELED);
        setContentView(R.layout.activity_note_list_configuration);
        setContentView(R.layout.activity_note_list_configuration);


        if (!(NoteServerSyncHelper.isConfigured(this))) {
        db = NoteSQLiteOpenHelper.getInstance(this);
        try {
            this.localAccount = db.getLocalAccountByAccountName(SingleAccountHelper.getCurrentSingleSignOnAccount(this).name);
        } catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
            e.printStackTrace();
            Toast.makeText(this, R.string.widget_not_logged_in, Toast.LENGTH_LONG).show();
            Toast.makeText(this, R.string.widget_not_logged_in, Toast.LENGTH_LONG).show();

            // TODO Present user with app login screen
            // TODO Present user with app login screen
            Log.w(TAG, "onCreate: user not logged in");
            Log.w(TAG, "onCreate: user not logged in");
            finish();
            finish();
        }
        }

        db = NoteSQLiteOpenHelper.getInstance(this);
        final Bundle extras = getIntent().getExtras();
        final Bundle extras = getIntent().getExtras();


        if (extras != null) {
        if (extras != null) {
@@ -117,16 +125,17 @@ public class NoteListWidgetConfiguration extends AppCompatActivity {
    @Override
    @Override
    protected void onResume() {
    protected void onResume() {
        super.onResume();
        super.onResume();

        new LoadCategoryListTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        new LoadCategoryListTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }
    }


    private class LoadCategoryListTask extends AsyncTask<Void, Void, List<NavigationAdapter.NavigationItem>> {
    private class LoadCategoryListTask extends AsyncTask<Void, Void, List<NavigationAdapter.NavigationItem>> {
        @Override
        @Override
        protected List<NavigationAdapter.NavigationItem> doInBackground(Void... voids) {
        protected List<NavigationAdapter.NavigationItem> doInBackground(Void... voids) {
            if (localAccount == null) {
                return new ArrayList<>();
            }
            NavigationAdapter.NavigationItem itemUncategorized;
            NavigationAdapter.NavigationItem itemUncategorized;
            // FIXME hardcoded accountId
            List<NavigationAdapter.NavigationItem> categories = db.getCategories(localAccount.getId());
            List<NavigationAdapter.NavigationItem> categories = db.getCategories(1);


            if (!categories.isEmpty() && categories.get(0).label.isEmpty()) {
            if (!categories.isEmpty() && categories.get(0).label.isEmpty()) {
                itemUncategorized = categories.get(0);
                itemUncategorized = categories.get(0);
@@ -134,8 +143,7 @@ public class NoteListWidgetConfiguration extends AppCompatActivity {
                itemUncategorized.icon = NavigationAdapter.ICON_NOFOLDER;
                itemUncategorized.icon = NavigationAdapter.ICON_NOFOLDER;
            }
            }


            // FIXME hardcoded accountId
            Map<String, Integer> favorites = db.getFavoritesCount(localAccount.getId());
            Map<String, Integer> favorites = db.getFavoritesCount(1);
            int numFavorites = favorites.containsKey("1") ? favorites.get("1") : 0;
            int numFavorites = favorites.containsKey("1") ? favorites.get("1") : 0;
            int numNonFavorites = favorites.containsKey("0") ? favorites.get("0") : 0;
            int numNonFavorites = favorites.containsKey("0") ? favorites.get("0") : 0;
            itemFavorites.count = numFavorites;
            itemFavorites.count = numFavorites;
+5 −5
Original line number Original line Diff line number Diff line
@@ -40,7 +40,11 @@ public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFact
        displayMode = sp.getInt(NoteListWidget.WIDGET_MODE_KEY + appWidgetId, -1);
        displayMode = sp.getInt(NoteListWidget.WIDGET_MODE_KEY + appWidgetId, -1);
        darkTheme = sp.getBoolean(NoteListWidget.DARK_THEME_KEY + appWidgetId, false);
        darkTheme = sp.getBoolean(NoteListWidget.DARK_THEME_KEY + appWidgetId, false);
        category = sp.getString(NoteListWidget.WIDGET_CATEGORY_KEY + appWidgetId, "");
        category = sp.getString(NoteListWidget.WIDGET_CATEGORY_KEY + appWidgetId, "");
    }


    @Override
    public void onCreate() {
        db = NoteSQLiteOpenHelper.getInstance(context);
        try {
        try {
            accountId = db.getLocalAccountByAccountName(SingleAccountHelper.getCurrentSingleSignOnAccount(context).name).getId();
            accountId = db.getLocalAccountByAccountName(SingleAccountHelper.getCurrentSingleSignOnAccount(context).name).getId();
        } catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
        } catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
@@ -48,11 +52,6 @@ public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFact
        }
        }
    }
    }


    @Override
    public void onCreate() {
        db = NoteSQLiteOpenHelper.getInstance(context);
    }

    @Override
    @Override
    public void onDataSetChanged() {
    public void onDataSetChanged() {
        if (displayMode == NoteListWidget.NLW_DISPLAY_ALL) {
        if (displayMode == NoteListWidget.NLW_DISPLAY_ALL) {
@@ -96,6 +95,7 @@ public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFact
        final Bundle extras = new Bundle();
        final Bundle extras = new Bundle();


        extras.putLong(EditNoteActivity.PARAM_NOTE_ID, note.getId());
        extras.putLong(EditNoteActivity.PARAM_NOTE_ID, note.getId());
        extras.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, note.getAccountId());
        fillInIntent.putExtras(extras);
        fillInIntent.putExtras(extras);
        fillInIntent.setData(Uri.parse(fillInIntent.toUri(Intent.URI_INTENT_SCHEME)));
        fillInIntent.setData(Uri.parse(fillInIntent.toUri(Intent.URI_INTENT_SCHEME)));


+8 −0
Original line number Original line Diff line number Diff line
@@ -54,6 +54,7 @@ public abstract class BaseNoteFragment extends Fragment implements CategoryDialo


    private static final int MENU_ID_PIN = -1;
    private static final int MENU_ID_PIN = -1;
    public static final String PARAM_NOTE_ID = "noteId";
    public static final String PARAM_NOTE_ID = "noteId";
    public static final String PARAM_ACCOUNT_ID = "accountId";
    public static final String PARAM_NEWNOTE = "newNote";
    public static final String PARAM_NEWNOTE = "newNote";
    private static final String SAVEDKEY_NOTE = "note";
    private static final String SAVEDKEY_NOTE = "note";
    private static final String SAVEDKEY_ORIGINAL_NOTE = "original_note";
    private static final String SAVEDKEY_ORIGINAL_NOTE = "original_note";
@@ -101,6 +102,13 @@ public abstract class BaseNoteFragment extends Fragment implements CategoryDialo
            isNew = true;
            isNew = true;
            long id = getArguments().getLong(PARAM_NOTE_ID);
            long id = getArguments().getLong(PARAM_NOTE_ID);
            if (id > 0) {
            if (id > 0) {
                long accountId = getArguments().getLong(PARAM_ACCOUNT_ID);
                if(accountId > 0) {
                    /* Switch account if account id has been provided */
                    this.localAccount = db.getAccount(accountId);
                    SingleAccountHelper.setCurrentAccount(getActivity().getApplicationContext(), localAccount.getAccountName());
                    db.getNoteServerSyncHelper().updateAccount();
                }
                note = originalNote = db.getNote(localAccount.getId(), id);
                note = originalNote = db.getNote(localAccount.getId(), id);
            } else {
            } else {
                CloudNote cloudNote = (CloudNote) getArguments().getSerializable(PARAM_NEWNOTE);
                CloudNote cloudNote = (CloudNote) getArguments().getSerializable(PARAM_NEWNOTE);
+2 −1
Original line number Original line Diff line number Diff line
@@ -74,10 +74,11 @@ public class NoteEditFragment extends BaseNoteFragment {
        }
        }
    };
    };


    public static NoteEditFragment newInstance(long noteId) {
    public static NoteEditFragment newInstance(long accountId, long noteId) {
        NoteEditFragment f = new NoteEditFragment();
        NoteEditFragment f = new NoteEditFragment();
        Bundle b = new Bundle();
        Bundle b = new Bundle();
        b.putLong(PARAM_NOTE_ID, noteId);
        b.putLong(PARAM_NOTE_ID, noteId);
        b.putLong(PARAM_ACCOUNT_ID, accountId);
        f.setArguments(b);
        f.setArguments(b);
        return f;
        return f;
    }
    }
Loading