0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-20 20:03:05 +02:00

Implement Shortcut option without opening app

This commit is contained in:
Akshay 2021-04-19 22:49:39 +05:30 committed by Mike Hardy
parent c3041b7114
commit c876b8f25a

View File

@ -15,9 +15,15 @@
****************************************************************************************/
package com.ichi2.anki;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.content.res.Configuration;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import com.google.android.material.navigation.NavigationView;
@ -31,13 +37,18 @@ import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.SwitchCompat;
import androidx.appcompat.widget.Toolbar;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.ichi2.anki.dialogs.HelpDialog;
import com.ichi2.libanki.stats.Stats;
import com.ichi2.themes.Themes;
import java.util.Arrays;
import androidx.drawerlayout.widget.ClosableDrawerLayout;
import androidx.drawerlayout.widget.DrawerLayout;
@ -131,6 +142,56 @@ public abstract class NavigationDrawerActivity extends AnkiActivity implements N
}
mDrawerToggle.setDrawerSlideAnimationEnabled(animationEnabled());
mDrawerLayout.addDrawerListener(mDrawerToggle);
enablePostShortcut(this);
}
@TargetApi(Build.VERSION_CODES.N_MR1)
public static void enablePostShortcut(@NonNull Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
return;
}
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
// Review Cards Shortcut
Intent intentReviewCards = new Intent(context, Reviewer.class);
intentReviewCards.setAction(Intent.ACTION_VIEW);
intentReviewCards.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
ShortcutInfo reviewCardsShortcut = new ShortcutInfo.Builder(context, "reviewCardsShortcutId")
.setShortLabel(context.getString(R.string.card_info_reviews))
.setLongLabel(context.getString(R.string.card_info_reviews))
.setIcon(Icon.createWithResource(context, R.drawable.ankidroid_logo))
.setIntent(intentReviewCards)
.build();
// Add Note Shortcut
Intent intentAddNote = new Intent(context, NoteEditor.class);
intentAddNote.setAction(Intent.ACTION_VIEW);
intentAddNote.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intentAddNote.putExtra(NoteEditor.EXTRA_CALLER, NoteEditor.CALLER_DECKPICKER);
ShortcutInfo NoteEditorShortcut = new ShortcutInfo.Builder(context, "noteEditorShortcutId")
.setShortLabel(context.getString(R.string.menu_add_note))
.setLongLabel(context.getString(R.string.menu_add_note))
.setIcon(Icon.createWithResource(context, R.drawable.ankidroid_logo))
.setIntent(intentAddNote)
.build();
// CardBrowser Shortcut
Intent intentCardBrowser = new Intent(context, CardBrowser.class);
intentCardBrowser.setAction(Intent.ACTION_VIEW);
intentCardBrowser.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
ShortcutInfo cardBrowserShortcut = new ShortcutInfo.Builder(context, "cardBrowserShortcutId")
.setShortLabel(context.getString(R.string.card_browser))
.setLongLabel(context.getString(R.string.card_browser))
.setIcon(Icon.createWithResource(context, R.drawable.ankidroid_logo))
.setIntent(intentCardBrowser)
.build();
shortcutManager.addDynamicShortcuts(Arrays.asList(reviewCardsShortcut, NoteEditorShortcut, cardBrowserShortcut));
}