Stop importing android.os.Build 56b468ecbb

This commit is contained in:
inotia00 2023-12-24 02:21:27 +09:00 • committed by Cuynu
commit 4cf763d832
13 changed files with 118 additions and 47 deletions

View file

@ -100,6 +100,26 @@ public class DataHolder extends AutoSafeParcelable implements Closeable {
return new Builder(columns);
}
<<<<<<< HEAD
=======
/**
* @param statusCode The status code of this {@link DataHolder}.
* @param metadata The metadata associated with this {@link DataHolder} (may be null).
* @return An empty {@link DataHolder} object with the given status and metadata.
*/
public static DataHolder empty(int statusCode, Bundle metadata) {
return new DataHolder(new String[0], new CursorWindow[0], statusCode, metadata);
}
/**
* @param statusCode The status code of this {@link DataHolder}.
* @return An empty {@link DataHolder} object with the given status and null metadata.
*/
public static DataHolder empty(int statusCode) {
return empty(statusCode, null);
}
>>>>>>> b20726b4 (Stop importing android.os.Build https://github.com/microg/GmsCore/commit/56b468ecbb450844c3e41a2b7d85ff979d98734b)
@SuppressLint({"NewApi", "ObsoleteSdkInt"})
static int getCursorType(Cursor cursor, int i) {
return cursor.getType(i);

View file

@ -26,6 +26,8 @@ import android.opengl.GLES10;
import android.os.Build;
import android.util.DisplayMetrics;
import org.microg.gms.profile.ProfileManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@ -56,6 +58,7 @@ public class DeviceConfiguration {
public int widthPixels;
public DeviceConfiguration(Context context) {
ProfileManager.ensureInitialized(context);
ConfigurationInfo configurationInfo = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getDeviceConfigurationInfo();
touchScreen = configurationInfo.reqTouchScreen;
keyboardType = configurationInfo.reqKeyboardType;
@ -78,10 +81,10 @@ public class DeviceConfiguration {
}
Collections.sort(sharedLibraries);
availableFeatures = new ArrayList<>();
if (packageManager.getSystemAvailableFeatures() != null) {
for (FeatureInfo featureInfo : packageManager.getSystemAvailableFeatures()) {
if (featureInfo != null && featureInfo.name != null) availableFeatures.add(featureInfo.name);
}
packageManager.getSystemAvailableFeatures();
for (FeatureInfo featureInfo : packageManager.getSystemAvailableFeatures()) {
if (featureInfo != null && featureInfo.name != null)
availableFeatures.add(featureInfo.name);
}
Collections.sort(availableFeatures);
this.nativePlatforms = getNativePlatforms();
@ -98,9 +101,12 @@ public class DeviceConfiguration {
Collections.sort(this.glExtensions);
}
@SuppressWarnings({"deprecation", "InlinedApi"})
@SuppressWarnings({"InlinedApi"})
private static List<String> getNativePlatforms() {
<<<<<<< HEAD
List<String> nativePlatforms;
=======
>>>>>>> b20726b4 (Stop importing android.os.Build https://github.com/microg/GmsCore/commit/56b468ecbb450844c3e41a2b7d85ff979d98734b)
return Arrays.asList(Build.SUPPORTED_ABIS);
}

View file

@ -1,5 +1,10 @@
package org.microg.gms.common;
<<<<<<< HEAD
=======
import static android.os.Build.VERSION.SDK_INT;
>>>>>>> b20726b4 (Stop importing android.os.Build https://github.com/microg/GmsCore/commit/56b468ecbb450844c3e41a2b7d85ff979d98734b)
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
@ -11,7 +16,6 @@ import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.PowerManager;
import android.provider.Settings;
import android.util.Log;
@ -33,7 +37,7 @@ public class ForegroundServiceContext extends ContextWrapper {
if (!isIgnoringBatteryOptimizations()) {
Log.d(TAG, "Starting in foreground mode.");
service.putExtra(EXTRA_FOREGROUND, true);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (SDK_INT >= 26) {
return super.startForegroundService(service);
} else {
return super.startService(service);
@ -75,7 +79,7 @@ public class ForegroundServiceContext extends ContextWrapper {
String serviceName = getServiceName(service);
Log.d(tag, "Started " + serviceName + " in foreground mode.");
try {
Notification notification = buildForegroundNotification(service, serviceName);
Notification notification = buildForegroundNotification(service);
service.startForeground(serviceName.hashCode(), notification);
Log.d(tag, "Notification: " + notification);
} catch (Exception e) {
@ -85,9 +89,13 @@ public class ForegroundServiceContext extends ContextWrapper {
}
@SuppressLint("BatteryLife")
<<<<<<< HEAD
private static Notification buildForegroundNotification(Context context, String serviceName) {
=======
private static Notification buildForegroundNotification(Context context) {
>>>>>>> b20726b4 (Stop importing android.os.Build https://github.com/microg/GmsCore/commit/56b468ecbb450844c3e41a2b7d85ff979d98734b)
Intent notificationIntent = new Intent();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (SDK_INT >= 31) {
notificationIntent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
notificationIntent.setData(Uri.parse("package:" + context.getPackageName()));
} else {
@ -97,8 +105,8 @@ public class ForegroundServiceContext extends ContextWrapper {
PendingIntent notificationPendingIntent = PendingIntent.getActivity(context,
0,
notificationIntent,
0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
PendingIntent.FLAG_IMMUTABLE);
if (SDK_INT >= 26) {
NotificationChannel Channel = new NotificationChannel("foreground-service",
context.getResources().getString(R.string.notification_service_name),
NotificationManager.IMPORTANCE_LOW);

View file

@ -33,8 +33,6 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH;
import static org.microg.gms.common.Constants.GMS_PACKAGE_NAME;
public class MultiConnectionKeeper {
@ -164,10 +162,7 @@ public class MultiConnectionKeeper {
} else {
intent = gmsIntent;
}
int flags = Context.BIND_AUTO_CREATE | Context.BIND_DEBUG_UNBIND;
if (SDK_INT >= ICE_CREAM_SANDWICH) {
flags |= Context.BIND_ADJUST_WITH_ACTIVITY;
}
int flags = Context.BIND_AUTO_CREATE | Context.BIND_DEBUG_UNBIND | Context.BIND_ADJUST_WITH_ACTIVITY;
bound = context.bindService(intent, serviceConnection, flags);
Log.d(TAG, "Connection(" + actionString + ") : bind() : bindService=" + bound);
if (!bound) {

View file

@ -5,11 +5,12 @@
package com.google.android.gms.common.security;
import static android.os.Build.VERSION.SDK_INT;
import static com.google.android.gms.security.ProviderInstaller.PROVIDER_NAME;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Build;
import android.os.Process;
import android.util.Log;
@ -53,13 +54,14 @@ public class ProviderInstallerImpl {
String packageName = PackageUtils.packageFromProcessId(context, Process.myPid());
if (packageName != null && packageName.contains(".")) return packageName;
try {
@SuppressLint("DiscouragedPrivateApi")
Method getBasePackageName = Context.class.getDeclaredMethod("getBasePackageName");
packageName = (String) getBasePackageName.invoke(context);
if (packageName != null) return packageName;
} catch (Exception e) {
} catch (Exception ignored) {
}
if (Build.VERSION.SDK_INT >= 29) {
if (SDK_INT >= 29) {
return context.getOpPackageName();
}
Context applicationContext = context.getApplicationContext();

View file

@ -17,6 +17,7 @@
package org.microg.gms.auth;
import static android.accounts.AccountManager.VISIBILITY_VISIBLE;
import static android.os.Build.VERSION.SDK_INT;
import android.Manifest;
import android.accounts.Account;
@ -27,7 +28,6 @@ import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
@ -36,6 +36,7 @@ import androidx.annotation.Nullable;
import org.microg.gms.common.PackageUtils;
import java.util.Arrays;
import java.util.Objects;
import static org.microg.gms.auth.AuthConstants.DEFAULT_ACCOUNT_TYPE;
import static org.microg.gms.auth.AuthConstants.PROVIDER_EXTRA_ACCOUNTS;
@ -54,11 +55,15 @@ public class AccountContentProvider extends ContentProvider {
@Nullable
@Override
public Bundle call(String method, String arg, Bundle extras) {
<<<<<<< HEAD
String suggestedPackageName;
suggestedPackageName = getCallingPackage();
=======
String suggestedPackageName = getCallingPackage();
>>>>>>> b20726b4 (Stop importing android.os.Build https://github.com/microg/GmsCore/commit/56b468ecbb450844c3e41a2b7d85ff979d98734b)
String packageName = PackageUtils.getAndCheckCallingPackage(getContext(), suggestedPackageName);
Log.d(TAG, "Call " + method + " from " + packageName + " with arg " + arg);
if (!PackageUtils.callerHasExtendedAccess(getContext())) {
if (!PackageUtils.callerHasExtendedAccess(Objects.requireNonNull(getContext()))) {
String[] packagesForUid = getContext().getPackageManager().getPackagesForUid(Binder.getCallingUid());
if (packagesForUid != null && packagesForUid.length != 0)
Log.w(TAG, "Not granting extended access to " + Arrays.toString(packagesForUid)
@ -72,10 +77,14 @@ public class AccountContentProvider extends ContentProvider {
if (arg != null && (arg.equals(DEFAULT_ACCOUNT_TYPE) || arg.startsWith(DEFAULT_ACCOUNT_TYPE + "."))) {
AccountManager am = AccountManager.get(getContext());
accounts = am.getAccountsByTypeForPackage(arg, packageName);
<<<<<<< HEAD
if (accounts == null || accounts.length == 0) {
=======
if (accounts.length == 0) {
>>>>>>> b20726b4 (Stop importing android.os.Build https://github.com/microg/GmsCore/commit/56b468ecbb450844c3e41a2b7d85ff979d98734b)
accounts = am.getAccountsByType(arg);
}
if (Build.VERSION.SDK_INT >= 26 && accounts != null && arg.equals(DEFAULT_ACCOUNT_TYPE)) {
if (SDK_INT >= 26 && arg.equals(DEFAULT_ACCOUNT_TYPE)) {
for (Account account : accounts) {
if (am.getAccountVisibility(account, packageName) == AccountManager.VISIBILITY_UNDEFINED) {
Log.d(TAG, "Make account " + account + " visible to " + packageName);

View file

@ -18,13 +18,13 @@ package org.microg.gms.auth;
import static android.content.pm.ApplicationInfo.FLAG_SYSTEM;
import static android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
import static android.os.Build.VERSION.SDK_INT;
import static org.microg.gms.auth.AuthPrefs.isTrustGooglePermitted;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;
import org.microg.gms.base.core.BuildConfig;
@ -105,7 +105,7 @@ public class AuthManager {
public void setPermitted(boolean value) {
setUserData(buildPermKey(), value ? "1" : "0");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && value && packageName != null) {
if (SDK_INT >= 26 && value && packageName != null) {
// Make account persistently visible as we already granted access
accountManager.setAccountVisibility(getAccount(), packageName, AccountManager.VISIBILITY_VISIBLE);
}
@ -170,7 +170,7 @@ public class AuthManager {
public void setAuthToken(String service, String auth) {
getAccountManager().setAuthToken(getAccount(), buildTokenKey(service), auth);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && packageName != null && auth != null) {
if (SDK_INT >= 26 && packageName != null && auth != null) {
// Make account persistently visible as we already granted access
accountManager.setAccountVisibility(getAccount(), packageName, AccountManager.VISIBILITY_VISIBLE);
}

View file

@ -18,7 +18,6 @@ package org.microg.gms.checkin;
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
import static android.os.Build.VERSION.SDK_INT;
import static org.microg.gms.checkin.CheckinService.EXTRA_FORCE_CHECKIN;
import static org.microg.gms.checkin.CheckinService.REGULAR_CHECKIN_INTERVAL;
@ -54,7 +53,7 @@ public class TriggerReceiver extends WakefulBroadcastReceiver {
Intent subIntent = new Intent(context, CheckinService.class);
subIntent.putExtra(EXTRA_FORCE_CHECKIN, force);
startWakefulService(new ForegroundServiceContext(context), subIntent);
} else if (SDK_INT >= 23) {
} else {
// no network, register a network callback to retry when we have internet
NetworkRequest networkRequest = new NetworkRequest.Builder()
.addCapability(NET_CAPABILITY_INTERNET)

View file

@ -10,7 +10,6 @@ import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;
import android.text.TextUtils;
import java.util.ArrayList;
@ -55,9 +54,7 @@ public class GcmDatabase extends SQLiteOpenHelper {
public GcmDatabase(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.context = context;
if (Build.VERSION.SDK_INT >= 16) {
this.setWriteAheadLoggingEnabled(true);
}
this.setWriteAheadLoggingEnabled(true);
}
public static class App {

View file

@ -19,8 +19,42 @@ package org.microg.gms.gcm;
import static android.app.AlarmManager.ELAPSED_REALTIME_WAKEUP;
import static android.os.Build.VERSION.SDK_INT;
import static org.microg.gms.common.PackageUtils.warnIfNotPersistentProcess;
import static org.microg.gms.gcm.GcmConstants.*;
import static org.microg.gms.gcm.McsConstants.*;
import static org.microg.gms.gcm.GcmConstants.ACTION_C2DM_RECEIVE;
import static org.microg.gms.gcm.GcmConstants.EXTRA_APP;
import static org.microg.gms.gcm.GcmConstants.EXTRA_APP_OVERRIDE;
import static org.microg.gms.gcm.GcmConstants.EXTRA_COLLAPSE_KEY;
import static org.microg.gms.gcm.GcmConstants.EXTRA_FROM;
import static org.microg.gms.gcm.GcmConstants.EXTRA_MESSAGE_ID;
import static org.microg.gms.gcm.GcmConstants.EXTRA_MESSENGER;
import static org.microg.gms.gcm.GcmConstants.EXTRA_RAWDATA;
import static org.microg.gms.gcm.GcmConstants.EXTRA_RAWDATA_BASE64;
import static org.microg.gms.gcm.GcmConstants.EXTRA_REGISTRATION_ID;
import static org.microg.gms.gcm.GcmConstants.EXTRA_SEND_FROM;
import static org.microg.gms.gcm.GcmConstants.EXTRA_SEND_TO;
import static org.microg.gms.gcm.GcmConstants.EXTRA_TTL;
import static org.microg.gms.gcm.McsConstants.ACTION_ACK;
import static org.microg.gms.gcm.McsConstants.ACTION_CONNECT;
import static org.microg.gms.gcm.McsConstants.ACTION_HEARTBEAT;
import static org.microg.gms.gcm.McsConstants.ACTION_RECONNECT;
import static org.microg.gms.gcm.McsConstants.ACTION_SEND;
import static org.microg.gms.gcm.McsConstants.EXTRA_REASON;
import static org.microg.gms.gcm.McsConstants.MCS_CLOSE_TAG;
import static org.microg.gms.gcm.McsConstants.MCS_DATA_MESSAGE_STANZA_TAG;
import static org.microg.gms.gcm.McsConstants.MCS_HEARTBEAT_ACK_TAG;
import static org.microg.gms.gcm.McsConstants.MCS_HEARTBEAT_PING_TAG;
import static org.microg.gms.gcm.McsConstants.MCS_IQ_STANZA_TAG;
import static org.microg.gms.gcm.McsConstants.MCS_LOGIN_REQUEST_TAG;
import static org.microg.gms.gcm.McsConstants.MCS_LOGIN_RESPONSE_TAG;
import static org.microg.gms.gcm.McsConstants.MSG_ACK;
import static org.microg.gms.gcm.McsConstants.MSG_CONNECT;
import static org.microg.gms.gcm.McsConstants.MSG_HEARTBEAT;
import static org.microg.gms.gcm.McsConstants.MSG_INPUT;
import static org.microg.gms.gcm.McsConstants.MSG_INPUT_ERROR;
import static org.microg.gms.gcm.McsConstants.MSG_OUTPUT;
import static org.microg.gms.gcm.McsConstants.MSG_OUTPUT_DONE;
import static org.microg.gms.gcm.McsConstants.MSG_OUTPUT_ERROR;
import static org.microg.gms.gcm.McsConstants.MSG_OUTPUT_READY;
import static org.microg.gms.gcm.McsConstants.MSG_TEARDOWN;
import android.annotation.SuppressLint;
import android.app.AlarmManager;
@ -34,7 +68,6 @@ import android.content.pm.PermissionInfo;
import android.content.pm.ResolveInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
@ -67,7 +100,6 @@ import org.microg.gms.gcm.mcs.IqStanza;
import org.microg.gms.gcm.mcs.LoginRequest;
import org.microg.gms.gcm.mcs.LoginResponse;
import org.microg.gms.gcm.mcs.Setting;
import org.microg.mgms.settings.SettingsProvider;
import java.io.Closeable;
import java.lang.reflect.Field;
@ -138,10 +170,10 @@ public class McsService extends Service implements Handler.Callback {
@Nullable
private Method addPowerSaveTempWhitelistAppMethod;
@Nullable
@RequiresApi(Build.VERSION_CODES.S)
@RequiresApi(31)
private Object powerExemptionManager;
@Nullable
@RequiresApi(Build.VERSION_CODES.S)
@RequiresApi(31)
private Method addToTemporaryAllowListMethod;
private class HandlerThread extends Thread {
@ -156,7 +188,7 @@ public class McsService extends Service implements Handler.Callback {
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "mcs");
wakeLock.setReferenceCounted(false);
synchronized (McsService.class) {
rootHandler = new Handler(Looper.myLooper(), McsService.this);
rootHandler = new Handler(Objects.requireNonNull(Looper.myLooper()), McsService.this);
if (connectIntent != null) {
rootHandler.sendMessage(rootHandler.obtainMessage(MSG_CONNECT, connectIntent));
WakefulBroadcastReceiver.completeWakefulIntent(connectIntent);
@ -181,7 +213,7 @@ public class McsService extends Service implements Handler.Callback {
powerManager = (PowerManager) getSystemService(POWER_SERVICE);
if (checkSelfPermission("android.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST") == PackageManager.PERMISSION_GRANTED) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (SDK_INT >= 31) {
Class<?> powerExemptionManagerClass = Class.forName("android.os.PowerExemptionManager");
powerExemptionManager = getSystemService(powerExemptionManagerClass);
addToTemporaryAllowListMethod =
@ -280,7 +312,10 @@ public class McsService extends Service implements Handler.Callback {
closeAll();
}
logd(context, "Scheduling heartbeat in " + heartbeatMs / 1000 + " seconds...");
<<<<<<< HEAD
// This is supposed to work even when running in idle and without battery optimization disabled
=======
>>>>>>> b20726b4 (Stop importing android.os.Build https://github.com/microg/GmsCore/commit/56b468ecbb450844c3e41a2b7d85ff979d98734b)
alarmManager.setExactAndAllowWhileIdle(ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + heartbeatMs, heartbeatIntent);
}
@ -593,7 +628,7 @@ public class McsService extends Service implements Handler.Callback {
}
private void addPowerSaveTempWhitelistApp(String packageName) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (SDK_INT >= 31) {
try {
if (addToTemporaryAllowListMethod != null && powerExemptionManager != null) {
logd(this, "Adding app " + packageName + " to the temp allowlist");

View file

@ -17,7 +17,6 @@
package org.microg.gms.gcm;
import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.N;
import static org.microg.gms.gcm.McsConstants.ACTION_CONNECT;
import static org.microg.gms.gcm.McsConstants.ACTION_HEARTBEAT;
import static org.microg.gms.gcm.McsConstants.EXTRA_REASON;
@ -44,7 +43,7 @@ public class TriggerReceiver extends WakefulBroadcastReceiver {
* "Project Svelte" is just there to f**k things up...
*/
public synchronized static void register(Context context) {
if (SDK_INT >= N && !registered) {
if (SDK_INT >= 24 && !registered) {
IntentFilter intentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
context.getApplicationContext().registerReceiver(new TriggerReceiver(), intentFilter);
registered = true;

View file

@ -18,7 +18,6 @@ package org.microg.gms.ui;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.Nullable;
@ -34,6 +33,7 @@ import org.microg.tools.ui.ResourceSettingsFragment;
import static android.accounts.AccountManager.PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE;
import static android.accounts.AccountManager.VISIBILITY_NOT_VISIBLE;
import static android.accounts.AccountManager.VISIBILITY_VISIBLE;
import static android.os.Build.VERSION.SDK_INT;
import static org.microg.gms.auth.AuthManager.PREF_AUTH_VISIBLE;
public class AccountSettingsActivity extends AbstractSettingsActivity {
@ -53,7 +53,7 @@ public class AccountSettingsActivity extends AbstractSettingsActivity {
super.onCreatePreferences(savedInstanceState, rootKey);
Preference pref = findPreference(PREF_AUTH_VISIBLE);
if (pref != null) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
if (SDK_INT < 26) {
pref.setVisible(false);
} else {
pref.setOnPreferenceChangeListener((preference, newValue) -> {

View file

@ -1,6 +1,7 @@
package org.microg.gms.ui;
import android.os.Build;
import static android.os.Build.VERSION.SDK_INT;
import android.os.Bundle;
import androidx.annotation.Nullable;
@ -26,7 +27,7 @@ public class SettingsActivity extends AppCompatActivity {
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_root_activity);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (SDK_INT >= 31) {
DynamicColors.applyToActivityIfAvailable(this);
}
appBarConfiguration = new AppBarConfiguration.Builder(getNavController().getGraph()).build();