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

Unverified Commit 74738bc6 authored by alperozturk's avatar alperozturk
Browse files

NoteListWidgetFactory npe checks

parent fb9788dc
Loading
Loading
Loading
Loading
+198 −156
Original line number Original line Diff line number Diff line
/*
/*
 * Nextcloud Notes - Android Client
 * Nextcloud Notes - Android Client
 *
 *
 * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2017-2025 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: GPL-3.0-or-later
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
 */
package it.niedermann.owncloud.notes.widget.notelist;
package it.niedermann.owncloud.notes.widget.notelist


import static it.niedermann.owncloud.notes.edit.EditNoteActivity.PARAM_CATEGORY;
import android.appwidget.AppWidgetManager
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_ALL;
import android.content.ComponentName
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_CATEGORY;
import android.content.Context
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_STARRED;
import android.content.Intent

import android.os.Bundle
import android.appwidget.AppWidgetManager;
import android.util.Log
import android.content.ComponentName;
import android.widget.RemoteViews
import android.content.Context;
import android.widget.RemoteViewsService.RemoteViewsFactory
import android.content.Intent;
import androidx.core.content.ContextCompat
import android.net.Uri;
import it.niedermann.owncloud.notes.R
import android.os.Bundle;
import it.niedermann.owncloud.notes.edit.EditNoteActivity
import android.util.Log;
import it.niedermann.owncloud.notes.main.MainActivity
import android.widget.RemoteViews;
import it.niedermann.owncloud.notes.persistence.NotesRepository
import android.widget.RemoteViewsService;
import it.niedermann.owncloud.notes.persistence.entity.Account

import it.niedermann.owncloud.notes.persistence.entity.Note
import androidx.annotation.NonNull;
import it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData
import androidx.core.content.ContextCompat;
import it.niedermann.owncloud.notes.shared.model.ENavigationCategoryType

import it.niedermann.owncloud.notes.shared.model.NavigationCategory
import java.util.ArrayList;
import it.niedermann.owncloud.notes.shared.util.NotesColorUtil
import java.util.List;
import androidx.core.net.toUri


import it.niedermann.owncloud.notes.R;
class NoteListWidgetFactory internal constructor(private val context: Context, intent: Intent) :
import it.niedermann.owncloud.notes.edit.EditNoteActivity;
    RemoteViewsFactory {
import it.niedermann.owncloud.notes.main.MainActivity;
    private val appWidgetId: Int = intent.getIntExtra(
import it.niedermann.owncloud.notes.persistence.NotesRepository;
        AppWidgetManager.EXTRA_APPWIDGET_ID,
import it.niedermann.owncloud.notes.persistence.entity.Account;
        AppWidgetManager.INVALID_APPWIDGET_ID
import it.niedermann.owncloud.notes.persistence.entity.Note;
    )
import it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData;
    private val repo: NotesRepository = NotesRepository.getInstance(context)
import it.niedermann.owncloud.notes.shared.model.ENavigationCategoryType;
    private val dbNotes: MutableList<Note> = ArrayList()
import it.niedermann.owncloud.notes.shared.model.NavigationCategory;
    private var data: NotesListWidgetData? = null
import it.niedermann.owncloud.notes.shared.util.NotesColorUtil;


    override fun onCreate() {
public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFactory {
    private static final String TAG = NoteListWidgetFactory.class.getSimpleName();

    private final Context context;
    private final int appWidgetId;
    private final NotesRepository repo;
    @NonNull
    private final List<Note> dbNotes = new ArrayList<>();
    private NotesListWidgetData data;

    NoteListWidgetFactory(Context context, Intent intent) {
        this.context = context;
        this.appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        repo = NotesRepository.getInstance(context);
    }

    @Override
    public void onCreate() {
        // Nothing to do here…
        // Nothing to do here…
    }
    }


    @Override
    override fun onDataSetChanged() {
    public void onDataSetChanged() {
        dbNotes.clear()
        dbNotes.clear();
        try {
        try {
            data = repo.getNoteListWidgetData(appWidgetId);
            data = repo.getNoteListWidgetData(appWidgetId)
            if (data == null) {
            if (data == null) {
                Log.w(TAG, "Widget data is null");
                Log.w(TAG, "Widget data is null")
                return;
                return
            }
            }

            val widgetData = data ?: return
            Log.v(TAG, "--- data - " + data);

            switch (data.getMode()) {
            Log.v(TAG, "--- data - $widgetData")
                case MODE_DISPLAY_ALL ->

                        dbNotes.addAll(repo.searchRecentByModified(data.getAccountId(), "%"));
            when (widgetData.mode) {
                case MODE_DISPLAY_STARRED ->
                NotesListWidgetData.MODE_DISPLAY_ALL -> dbNotes.addAll(
                        dbNotes.addAll(repo.searchFavoritesByModified(data.getAccountId(), "%"));
                    repo.searchRecentByModified(
                default -> {
                        widgetData.accountId, "%"
                    if (data.getCategory() != null) {
                    )
                        dbNotes.addAll(repo.searchCategoryByModified(data.getAccountId(), "%", data.getCategory()));
                )

                NotesListWidgetData.MODE_DISPLAY_STARRED -> dbNotes.addAll(
                    repo.searchFavoritesByModified(
                        widgetData.accountId, "%"
                    )
                )

                else -> {
                    if (widgetData.category != null) {
                        dbNotes.addAll(
                            repo.searchCategoryByModified(
                                widgetData.accountId,
                                "%",
                                widgetData.category
                            )
                        )
                    } else {
                    } else {
                        dbNotes.addAll(repo.searchUncategorizedByModified(data.getAccountId(), "%"));
                        dbNotes.addAll(
                            repo.searchUncategorizedByModified(
                                widgetData.accountId,
                                "%"
                            )
                        )
                    }
                    }
                }
                }
            }
            }
        } catch (Exception e) {
        } catch (e: Exception) {
            Log.w(TAG, "Error caught at onDataSetChanged: " + e);
            Log.w(TAG, "Error caught at onDataSetChanged: $e")
        }
        }
    }
    }


    @Override
    override fun onDestroy() {
    public void onDestroy() {
        //NoOp
        //NoOp
    }
    }


    @Override
    override fun getCount(): Int {
    public int getCount() {
        return dbNotes.size + 1
        return dbNotes.size() + 1;
    }
    }


    private Intent getEditNoteIntent(Bundle bundle) {
    private fun getEditNoteIntent(bundle: Bundle): Intent {
        final Intent intent = new Intent(context, EditNoteActivity.class);
        return Intent(context, EditNoteActivity::class.java).apply {
        intent.setPackage(context.getPackageName());
            setPackage(context.packageName)
        intent.putExtras(bundle);
            putExtras(bundle)
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
            setData(toUri(Intent.URI_INTENT_SCHEME).toUri())

        }
        return intent;
    }
    }


    private Intent getCreateNoteIntent(Account localAccount ) {
    private fun getCreateNoteIntent(localAccount: Account): Intent {
        final Bundle bundle = new Bundle();
        val bundle = Bundle()
        bundle.putSerializable(PARAM_CATEGORY, data.getMode() == MODE_DISPLAY_STARRED ? new NavigationCategory(ENavigationCategoryType.FAVORITES) : new NavigationCategory(localAccount.getId(), data.getCategory()));
        bundle.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, data.getAccountId());


        return getEditNoteIntent(bundle);
        data?.let {
    }
            val navigationCategory = if (it.mode == NotesListWidgetData.MODE_DISPLAY_STARRED) NavigationCategory(
                ENavigationCategoryType.FAVORITES
            ) else NavigationCategory(localAccount.id, it.category)


    private Intent getOpenNoteIntent(Note note) {
            bundle.putSerializable(EditNoteActivity.PARAM_CATEGORY, navigationCategory)
        final Bundle bundle = new Bundle();
            bundle.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, it.accountId)
        bundle.putLong(EditNoteActivity.PARAM_NOTE_ID, note.getId());
        }
        bundle.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, note.getAccountId());


        return getEditNoteIntent(bundle);
        return getEditNoteIntent(bundle)
    }
    }


    @Override
    private fun getOpenNoteIntent(note: Note): Intent {
    public RemoteViews getViewAt(int position) {
        val bundle = Bundle().apply {
        final RemoteViews note_content;
            putLong(EditNoteActivity.PARAM_NOTE_ID, note.id)
            putLong(EditNoteActivity.PARAM_ACCOUNT_ID, note.accountId)
        }


        if (position == 0) {
        return getEditNoteIntent(bundle)
            final Account localAccount = repo.getAccountById(data.getAccountId());

            final Intent createNoteIntent = getCreateNoteIntent(localAccount);
            final Intent openIntent = new Intent(Intent.ACTION_MAIN).setComponent(new ComponentName(context.getPackageName(), MainActivity.class.getName()));

            note_content = new RemoteViews(context.getPackageName(), R.layout.widget_entry_add);
            note_content.setOnClickFillInIntent(R.id.widget_entry_content_tv, openIntent);
            note_content.setOnClickFillInIntent(R.id.widget_entry_fav_icon, createNoteIntent);
            note_content.setTextViewText(R.id.widget_entry_content_tv, getCategoryTitle(context, data.getMode(), data.getCategory()));
            note_content.setImageViewResource(R.id.widget_entry_fav_icon, R.drawable.ic_add_blue_24dp);
            note_content.setInt(R.id.widget_entry_fav_icon, "setColorFilter", NotesColorUtil.contrastRatioIsSufficient(ContextCompat.getColor(context, R.color.widget_background), localAccount.getColor())
                    ? localAccount.getColor()
                    : ContextCompat.getColor(context, R.color.widget_foreground));
        } else {
            position--;
            if (position > dbNotes.size() - 1 || dbNotes.get(position) == null) {
                Log.e(TAG, "Could not find position \"" + position + "\" in dbNotes list.");
                return null;
    }
    }


            final Note note = dbNotes.get(position);
    private fun getRemoteViewFromData(): RemoteViews? {
            final Intent openNoteIntent = getOpenNoteIntent(note);
        val widgetData = data ?: return null

        val localAccount = repo.getAccountById(widgetData.accountId)
        val createNoteIntent = getCreateNoteIntent(localAccount)
        val openIntent = Intent(Intent.ACTION_MAIN).setComponent(
            ComponentName(
                context.packageName,
                MainActivity::class.java.getName()
            )
        )


            note_content = new RemoteViews(context.getPackageName(), R.layout.widget_entry);
        return RemoteViews(context.packageName, R.layout.widget_entry_add).apply {
            note_content.setOnClickFillInIntent(R.id.widget_note_list_entry, openNoteIntent);
            setOnClickFillInIntent(R.id.widget_entry_content_tv, openIntent)
            note_content.setTextViewText(R.id.widget_entry_content_tv, note.getTitle());
            setOnClickFillInIntent(R.id.widget_entry_fav_icon, createNoteIntent)
            note_content.setImageViewResource(R.id.widget_entry_fav_icon, note.getFavorite()
            setTextViewText(
                    ? R.drawable.ic_star_yellow_24dp
                R.id.widget_entry_content_tv,
                    : R.drawable.ic_star_grey_ccc_24dp);
                getCategoryTitle(context, widgetData.mode, widgetData.category)
            )
            setImageViewResource(
                R.id.widget_entry_fav_icon,
                R.drawable.ic_add_blue_24dp
            )
            setInt(
                R.id.widget_entry_fav_icon,
                "setColorFilter",
                if (NotesColorUtil.contrastRatioIsSufficient(
                        ContextCompat.getColor(
                            context,
                            R.color.widget_background
                        ), localAccount.color
                    )
                )
                    localAccount.color
                else
                    ContextCompat.getColor(context, R.color.widget_foreground)
            )
        }
    }
    }


        return note_content;
    private fun getRemoteViewFromPosition(position: Int): RemoteViews? {
        var position = position
        position--
        if (position < 0 || position >= dbNotes.size) {
            Log.e(TAG, "Could not find position \"$position\" in dbNotes list.")
            return null
        }


        val note = dbNotes[position]
        val openNoteIntent = getOpenNoteIntent(note)

        return RemoteViews(context.packageName, R.layout.widget_entry).apply {
            setOnClickFillInIntent(R.id.widget_note_list_entry, openNoteIntent)
            setTextViewText(R.id.widget_entry_content_tv, note.title)
            setImageViewResource(
                R.id.widget_entry_fav_icon, if (note.favorite)
                    R.drawable.ic_star_yellow_24dp
                else
                    R.drawable.ic_star_grey_ccc_24dp
            )
        }
    }
    }


    @NonNull
    override fun getViewAt(position: Int): RemoteViews? {
    private static String getCategoryTitle(@NonNull Context context, int displayMode, String category) {
        return if (position == 0 && data != null) {
        return switch (displayMode) {
            getRemoteViewFromData()
            case MODE_DISPLAY_STARRED ->
        } else {
                    context.getString(R.string.label_favorites);
            getRemoteViewFromPosition(position)
            case MODE_DISPLAY_CATEGORY ->
        }
                    "".equals(category)
                        ? context.getString(R.string.action_uncategorized)
                        : category;
            default -> context.getString(R.string.app_name);
        };
    }
    }


    @Override
    override fun getLoadingView(): RemoteViews? {
    public RemoteViews getLoadingView() {
        return null
        return null;
    }
    }


    @Override
    override fun getViewTypeCount(): Int {
    public int getViewTypeCount() {
        return 2
        return 2;
    }
    }


    @Override
    override fun getItemId(position: Int): Long {
    public long getItemId(int position) {
        var position = position
        if (position == 0) {
        if (position == 0) {
            return -1;
            return -1
        } else {
        } else {
            position--;
            position--
            if (position > dbNotes.size() - 1 || dbNotes.get(position) == null) {
            if (position > dbNotes.size - 1 || dbNotes.get(position) == null) {
                Log.e(TAG, "Could not find position \"" + position + "\" in dbNotes list.");
                Log.e(TAG, "Could not find position \"" + position + "\" in dbNotes list.")
                return -2;
                return -2
            }
            }
            return dbNotes.get(position).getId();
            return dbNotes[position].id
        }
        }
    }
    }


    @Override
    override fun hasStableIds(): Boolean {
    public boolean hasStableIds() {
        return true
        return true;
    }

    companion object {
        private val TAG: String = NoteListWidgetFactory::class.java.getSimpleName()

        private fun getCategoryTitle(
            context: Context,
            displayMode: Int,
            category: String?
        ): String {
            return when (displayMode) {
                NotesListWidgetData.MODE_DISPLAY_STARRED -> context.getString(R.string.label_favorites)
                NotesListWidgetData.MODE_DISPLAY_CATEGORY -> if ("" == category)
                    context.getString(R.string.action_uncategorized)
                else
                    category

                else -> context.getString(R.string.app_name)
            }!!
        }
    }
    }
}
}