Skip to content

Commit

Permalink
Dedicated class for grabbing context in python
Browse files Browse the repository at this point in the history
  • Loading branch information
bjester committed Jan 15, 2024
1 parent 21807eb commit 087d7ff
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.kivy.android;

import android.content.Context;

public class PythonContext {
public static PythonContext mInstance;

private final Context context;

private PythonContext(Context context) {
this.context = context;
}

public static PythonContext getInstance(Context context) {
if (mInstance == null) {
synchronized (PythonContext.class) {
if (mInstance == null) {
mInstance = new PythonContext(
context.getApplicationContext()
);
}
}
}
return PythonContext.mInstance;
}

public static Context get() {
if (PythonContext.mInstance == null) {
return null;
}
return PythonContext.mInstance.context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

import androidx.annotation.NonNull;

/**
* Ideally this would be called `PythonWorkerImpl` but the name is used in the native code.
*/
public class PythonWorker {
private static final String TAG = "PythonWorkerImpl";
// Python environment variables
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import androidx.core.app.NotificationManagerCompat;
import androidx.work.Configuration;

import org.kivy.android.PythonContext;
import org.learningequality.notification.NotificationRef;

import java.util.concurrent.Executors;
Expand All @@ -23,6 +24,8 @@ public class App extends Application implements Configuration.Provider {
@Override
public void onCreate() {
super.onCreate();
// Initialize Python context
PythonContext.getInstance(this);
createNotificationChannels();
// Register activity lifecycle callbacks
registerActivityLifecycleCallbacks(new KolibriActivityLifecycleCallbacks());
Expand Down
4 changes: 2 additions & 2 deletions src/android_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def get_version_name():

@cache
def get_context():
ContextUtil = autoclass("org.learningequality.ContextUtil")
return ContextUtil.getApplicationContext()
PythonContext = autoclass("org.kivy.android.PythonContext")
return PythonContext.get();


@cache
Expand Down

0 comments on commit 087d7ff

Please sign in to comment.