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

Commit 6d1c9edd authored by Frank Preel's avatar Frank Preel
Browse files

Try several contexts

parent ff9468bc
Loading
Loading
Loading
Loading
+39 −13
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@ import static com.android.inputmethod.latin.common.Constants.ImeOption.NO_MICROP
import android.Manifest.permission;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.pm.PackageManager;
import androidx.core.content.ContextCompat;
import android.app.ActivityOptions;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
@@ -877,7 +879,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen

        ImageButton floatingButton = view.findViewById(R.id.floating_button);
        if (floatingButton != null) {
            if (isSetupWizardFinished() && accountIsPremium()) {
            if (isSetupWizardFinished() && (accountIsPremium(getApplicationContext()) || accountIsPremiumFromIME())) {
                floatingButton.setVisibility(View.VISIBLE);
                floatingButton.setOnClickListener(new View.OnClickListener() {
                    @Override
@@ -903,25 +905,49 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
        ContentResolver contentResolver = mDisplayContext.getContentResolver();
        boolean isDeviceProvisioned = android.provider.Settings.Global.getInt(contentResolver,
            android.provider.Settings.Global.DEVICE_PROVISIONED, 0) == 1;
        Log.d(TAG, ">>>isDeviceProvisioned=" + isDeviceProvisioned);
        return isDeviceProvisioned;
    }

    private boolean accountIsPremium() {
        AccountManager accountManager = AccountManager.get(mDisplayContext);
    private boolean accountIsPremiumFromIME() {
        try {
            final Context secureContext = createPackageContext(
                "com.android.inputmethod.latin",
                Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY
            );
            Log.d(TAG, "Secur context: " + secureContext);
            return accountIsPremium(secureContext);
        } catch (PackageManager.NameNotFoundException e) {
            Log.wtf(TAG, "Error createPackageContext()", e);
            return accountIsPremium(this);
        }
    }

    private boolean accountIsPremium(Context context) {
        if(ContextCompat.checkSelfPermission(context, android.Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
            Log.e(TAG, "Missing permission");
            return false;
        }

        AccountManager accountManager = AccountManager.get(context);
        Log.d(TAG, "AccountManager: " + accountManager);

        Account[] accountList = accountManager.getAccountsByType("e.foundation.webdav.eelo");
        boolean accountIsPremium = false;
        Log.d(TAG, "Nb: " + accountList.length);
        
        if (accountList != null) {
        for (Account account : accountList) {
            Log.d(TAG, "Account: " + account.name);
            String groupData = accountManager.getUserData(account, "group");
            Log.d(TAG, "Group: " + groupData);
            if ("premium".equals(groupData)) {
                    accountIsPremium = true;
                    break;
                }
                Log.d(TAG, "Premium");
                return true;
            }
        }

        return accountIsPremium;
        Log.d(TAG, "No premium user");

        return false;
    }

    @Override