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

Commit 38364b79 authored by Marvin W.'s avatar Marvin W. 🐿️
Browse files

Adjust ContextContainer for M

parent 49755948
Loading
Loading
Loading
Loading
+110 −17
Original line number Diff line number Diff line
@@ -17,7 +17,15 @@
package org.microg.gms.maps;

import android.annotation.TargetApi;
import android.content.*;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentSender;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
@@ -36,7 +44,14 @@ import android.os.UserHandle;
import android.view.Display;
import android.view.ViewDebug;

import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * A hacked Context that allows access to gms resources but feels like the remote context for everything else.
@@ -563,4 +578,82 @@ public class ContextContainer extends Context {
    public Context createDisplayContext(Display display) {
        return original.createDisplayContext(display);
    }

    /* HIDDEN */

    public String getBasePackageName() {
        return (String) safeInvoke("getBasePackageName");
    }

    public String getOpPackageName() {
        return (String) safeInvoke("getOpPackageName");
    }

    public File getSharedPrefsFile(String name) {
        return (File) safeInvoke("getBasePackageName", String.class, name);
    }

    public void startActivityAsUser(Intent intent, UserHandle user) {
        safeInvoke("startActivityAsUser", Intent.class, UserHandle.class, intent, user);
    }

    public void startActivityAsUser(Intent intent, Bundle options, UserHandle userId) {
        safeInvoke("startActivityAsUser", Intent.class, Bundle.class, UserHandle.class, intent, options, userId);
    }

    public void startActivityForResult(String who, Intent intent, int requestCode, Bundle options) {
        safeInvoke("startActivityForResult", String.class, Intent.class, int.class, Bundle.class, who, intent, requestCode, options);
    }

    public boolean canStartActivityForResult() {
        return (Boolean) safeInvoke("canStartActivityForResult");
    }

    public void startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) {
        safeInvoke("startActivitiesAsUser", new Class[]{Intent[].class, Bundle.class, UserHandle.class}, intents, options, userHandle);
    }

    public void sendBroadcastMultiplePermissions(Intent intent, String[] receiverPermissions) {
        safeInvoke("sendBroadcastMultiplePermissions", Intent.class, String[].class, intent, receiverPermissions);
    }

    public void sendBroadcast(Intent intent, String receiverPermission, int appOp) {
        safeInvoke("sendBroadcast", Intent.class, String.class, int.class, intent, receiverPermission, appOp);
    }


    private Object safeInvoke(String name) {
        return safeInvoke(name, new Class[0]);
    }

    private <T1> Object safeInvoke(String name, Class<T1> t1Class, T1 t1Value) {
        return safeInvoke(name, new Class[]{t1Class}, t1Value);
    }

    private <T1, T2> Object safeInvoke(String name, Class<T1> t1Class, Class<T2> t2Class, T1 t1Value, T2 t2Value) {
        return safeInvoke(name, new Class[]{t1Class, t2Class}, t1Value, t2Value);
    }

    private <T1, T2, T3> Object safeInvoke(String name, Class<T1> t1Class, Class<T2> t2Class, Class<T3> t3Class, T1 t1Value, T2 t2Value, T3 t3Value) {
        return safeInvoke(name, new Class[]{t1Class, t2Class, t3Class}, t1Value, t2Value, t3Value);
    }

    private <T1, T2, T3, T4> Object safeInvoke(String name, Class<T1> t1Class, Class<T2> t2Class, Class<T3> t3Class, Class<T4> t4Class, T1 t1Value, T2 t2Value, T3 t3Value, T4 t4Value) {
        return safeInvoke(name, new Class[]{t1Class, t2Class, t3Class, t4Class}, t1Value, t2Value, t3Value, t4Value);
    }

    private Object safeInvoke(String name, Class[] classes, Object... values) {
        try {
            Method method = Context.class.getDeclaredMethod(name, classes);
            return method.invoke(original, values);
        } catch (InvocationTargetException e) {
            if (e.getTargetException() instanceof RuntimeException) {
                throw (RuntimeException) e.getTargetException();
            } else {
                throw new RuntimeException(e.getTargetException());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}