Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stats provider example #9

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ android {
signingConfig signingConfigs.release
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.ichi2.apisample" >
<uses-permission android:name="com.ichi2.anki.permission.READ_DATABASE"/>
<uses-permission android:name="com.ichi2.anki.permission.READ_WRITE_DATABASE"/>

<application
android:allowBackup="true"
android:fullBackupContent="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat" >
android:theme="@style/Theme.AppCompat"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
Expand All @@ -17,6 +21,10 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".StatsActivity"
android:label="@string/card_counts" >
</activity>
</application>

</manifest>
28 changes: 26 additions & 2 deletions app/src/main/java/com/ichi2/apisample/AnkiDroidHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public class AnkiDroidHelper {
private static final String DECK_REF_DB = "com.ichi2.anki.api.decks";
private static final String MODEL_REF_DB = "com.ichi2.anki.api.models";

// Temporary until new API is published and we can import static as with READ_WRITE_PERMISSION
public static final String READ_PERMISSION = "com.ichi2.anki.permission.READ_DATABASE";


private AddContentApi mApi;
private Context mContext;

Expand All @@ -46,22 +50,42 @@ public static boolean isApiAvailable(Context context) {
return AddContentApi.getAnkiDroidPackageName(context) != null;
}

/**
* Whether or not we should request read-only access to the AnkiDroid API
*/
public boolean shouldRequestReadPermission() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return false;
}
return shouldRequestReadWritePermission()
|| ContextCompat.checkSelfPermission(mContext, READ_PERMISSION) != PackageManager.PERMISSION_GRANTED;
}

/**
* Whether or not we should request full access to the AnkiDroid API
*/
public boolean shouldRequestPermission() {
public boolean shouldRequestReadWritePermission() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return false;
}
return ContextCompat.checkSelfPermission(mContext, READ_WRITE_PERMISSION) != PackageManager.PERMISSION_GRANTED;
}

/**
* Request permission from the user for read-only access to the AnkiDroid API (for SDK 23+)
* @param callbackActivity An Activity which implements onRequestPermissionsResult()
* @param callbackCode The callback code to be used in onRequestPermissionsResult()
*/
public void requestReadPermission(Activity callbackActivity, int callbackCode) {
ActivityCompat.requestPermissions(callbackActivity, new String[]{READ_PERMISSION}, callbackCode);
}

/**
* Request permission from the user to access the AnkiDroid API (for SDK 23+)
* @param callbackActivity An Activity which implements onRequestPermissionsResult()
* @param callbackCode The callback code to be used in onRequestPermissionsResult()
*/
public void requestPermission(Activity callbackActivity, int callbackCode) {
public void requestReadWritePermission(Activity callbackActivity, int callbackCode) {
ActivityCompat.requestPermissions(callbackActivity, new String[]{READ_WRITE_PERMISSION}, callbackCode);
}

Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/com/ichi2/apisample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public boolean onCreateOptionsMenu(Menu menu) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.stats_menu_item:
startActivity(new Intent(this, StatsActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
Expand Down Expand Up @@ -216,8 +219,8 @@ public boolean onMenuItemClick(MenuItem item) {
// Handle when the submenu items are clicked
if (item.getItemId() == ANKIDROID_INSTANT_ADD) {
// Request permission to access API if required
if (mAnkiDroid.shouldRequestPermission()) {
mAnkiDroid.requestPermission(MainActivity.this, AD_PERM_REQUEST);
if (mAnkiDroid.shouldRequestReadWritePermission()) {
mAnkiDroid.requestReadWritePermission(MainActivity.this, AD_PERM_REQUEST);
return true;
}
// Add all data using AnkiDroid provider
Expand Down
71 changes: 71 additions & 0 deletions app/src/main/java/com/ichi2/apisample/StatsActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.ichi2.apisample;

import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

public class StatsActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback {

private static final int AD_PERM_REQUEST = 0;
private AnkiDroidHelper mAnkiDroid;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mAnkiDroid = new AnkiDroidHelper(this);

setContentView(R.layout.activity_stats);
if (!AnkiDroidHelper.isApiAvailable(this)) {
Toast.makeText(this, R.string.api_not_available, Toast.LENGTH_LONG).show();
finishActivity(-1);
return;
}

if (mAnkiDroid.shouldRequestReadPermission()) {
mAnkiDroid.requestReadPermission(this, AD_PERM_REQUEST);
} else {
displayCardCount();
}
}

private void displayCardCount() {
TextView matureText = findViewById(R.id.count_mature);
TextView youngText = findViewById(R.id.count_young);
TextView newCount = findViewById(R.id.count_new);
TextView suspendedCount = findViewById(R.id.count_suspended);

Cursor countCursor = fetchCardCount();

matureText.setText(getString(R.string.mature_cards, countCursor.getInt(0)));
youngText.setText(getString(R.string.young_cards, countCursor.getInt(1)));
newCount.setText(getString(R.string.new_cards, countCursor.getInt(2)));
suspendedCount.setText(getString(R.string.suspended_cards, countCursor.getInt(3)));
}

private Cursor fetchCardCount() {
ContentResolver resolver = getApplicationContext().getContentResolver();
Cursor countCursor = resolver.query(Uri.parse("content://com.ichi2.anki.stats/cardcount"), null, null, null, null);
countCursor.moveToFirst();
return countCursor;
}


public void onRequestPermissionsResult (int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if ((requestCode == AD_PERM_REQUEST) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
displayCardCount();
} else {
Toast.makeText(this, R.string.permission_denied, Toast.LENGTH_LONG).show();
finishActivity(-1);
}
}
}
30 changes: 30 additions & 0 deletions app/src/main/res/layout/activity_stats.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_height="match_parent">
<TextView
android:id="@+id/count_mature"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="50dp"/>
<TextView
android:id="@+id/count_young"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="50dp"/>
<TextView
android:id="@+id/count_new"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="50dp"/>
<TextView
android:id="@+id/count_suspended"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="50dp"/>
</LinearLayout>
6 changes: 3 additions & 3 deletions app/src/main/res/menu/main_menu.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/settings"
<item android:id="@+id/stats_menu_item"
app:showAsAction="never"
android:visible="false"
android:title="@string/settings"/>
android:visible="true"
android:title="@string/card_counts"/>
</menu>
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@
<string name="n_items_selected">%d items selected</string>
<string name="n_items_added">%d items added to AnkiDroid</string>
<string name="card_add_fail">Error adding cards to AnkiDroid</string>
<string name="api_not_available">AnkiDroid API not available</string>
<string name="permission_denied">Couldn\'t get permission to access the AnkiDroid database</string>
<string name="card_counts">Card Counts</string>
<string name="mature_cards">Mature cards %d</string>
<string name="young_cards">Young cards %d</string>
<string name="new_cards">New cards %d</string>
<string name="suspended_cards">Suspended cards %d</string>
</resources>