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

Commit 629e86d6 authored by Rohit Sekhar's avatar Rohit Sekhar
Browse files

WIP: CountryCodeSetter

parent fd6cbbfb
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -133,6 +133,7 @@ BOARD_BOOTCONFIG += \
    androidboot.usbcontroller=a600000.dwc3 \
    androidboot.load_modules_parallel=true \
    androidboot.vendor.qspa=true
#BOARD_BOOTCONFIG += androidboot.selinux=permissive
BOARD_INCLUDE_DTB_IN_BOOTIMG := true
BOARD_INIT_BOOT_HEADER_VERSION := 4
BOARD_MKBOOTIMG_INIT_ARGS += --header_version $(BOARD_INIT_BOOT_HEADER_VERSION)
+7 −1
Original line number Diff line number Diff line
@@ -3,9 +3,11 @@
    package="org.lineageos.settings.device"
    android:sharedUserId="android.uid.system">

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.MANAGE_SENSOR_PRIVACY"/>
    <uses-permission android:name="android.permission.OBSERVE_SENSOR_PRIVACY"/>
    <uses-permission android:name="android.permission.REBOOT" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />

    <application
@@ -35,6 +37,10 @@
            android:exported="false"
            android:stopWithTask="false"/>

        <service
            android:name=".CountryCodeService"
            android:exported="false"
            android:stopWithTask="false"/>
    </application>

</manifest>
+5 −2
Original line number Diff line number Diff line
@@ -12,7 +12,10 @@ public class BootReceiver extends BroadcastReceiver {
        if (!intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
            return;

	Intent serviceIntent = new Intent(context, SliderSwitchHandlerService.class);
        context.startService(serviceIntent);
        Intent sliderServiceIntent = new Intent(context, SliderSwitchHandlerService.class);
        context.startService(sliderServiceIntent);

        Intent countryServiceIntent = new Intent(context, CountryCodeService.class);
        context.startService(countryServiceIntent);
    }
}
+114 −0
Original line number Diff line number Diff line
package org.lineageos.settings.device;

import android.app.Service;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;
import android.os.SystemProperties;
import android.util.Log;

public class CountryCodeService extends Service {
    private static final String TAG = "CountryCodeService";
    private static final int MSG_GET_COUNTRY_CODE = 1;
    private static final int MSG_REBOOT_LOAD_WIFI = 2;

    // Configuration constants
    private static final int MAX_RETRY_ATTEMPTS = 2;
    private static final int RETRY_DELAY_MS = 10 * 1000; // 10 seconds
    private static final int REBOOT_DELAY_MS = 3 * 1000; // 3 seconds

    private static final String PROPERTY_ODM_CCODE = "persist.odm.ccode";

    private final Handler mBackgroundHandler = new BackgroundHandler(Looper.getMainLooper());

    private PowerManager mPowerManager;
    private WifiManager mWifiManager;

    private static int mtrytimes = 0;

    private class BackgroundHandler extends Handler {

        public BackgroundHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_GET_COUNTRY_CODE:
                    mtrytimes++;
                    if (mtrytimes <= MAX_RETRY_ATTEMPTS) {
                        getWifiCountryCode();
                    } else {
                        Log.w(TAG, "Max retry attempts reached, setting ccode to 'other'");
                        SystemProperties.set(PROPERTY_ODM_CCODE, "other");
                        stopSelf();
                        return;
                    }
                    break;
                case MSG_REBOOT_LOAD_WIFI:
                    if (mPowerManager != null) {
                        Log.i(TAG, "Initiating reboot for WiFi configuration changes");
                        mPowerManager.reboot("Radio parameters changed to fcc");
                    } else {
                        Log.e(TAG, "PowerManager not available for reboot");
                    }
                    break;
            }
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "Service started");

        mWifiManager = (WifiManager) getSystemService(WifiManager.class);

        mPowerManager = (PowerManager) getSystemService(PowerManager.class);

        String odmCountryCode = SystemProperties.get(PROPERTY_ODM_CCODE, "");
        Log.d(TAG, "ODM country code from property: = " + odmCountryCode);
        if (odmCountryCode.isEmpty() || "other".equals(odmCountryCode)) {
            getWifiCountryCode();
        } else {
            Log.d(TAG, "Country code already set, no action needed");
            stopSelf();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        if (mBackgroundHandler != null) {
            mBackgroundHandler.removeCallbacksAndMessages(null);
            mBackgroundHandler.getLooper().quitSafely();
        }

        Log.d(TAG, "Service stopped");
        super.onDestroy();
    }

    private void getWifiCountryCode() {
        String countrycode = mWifiManager.getCountryCode();
        Log.d(TAG, "WiFi manager country code: " + countrycode);
        if (countrycode == null || countrycode.isEmpty()) {
            mBackgroundHandler.sendEmptyMessageDelayed(MSG_GET_COUNTRY_CODE, RETRY_DELAY_MS);
        } else {
            if ("IN".equals(countrycode) || "US".equals(countrycode) || "CA".equals(countrycode)) {
                SystemProperties.set(PROPERTY_ODM_CCODE, "fcc");
                mBackgroundHandler.sendEmptyMessageDelayed(MSG_REBOOT_LOAD_WIFI, REBOOT_DELAY_MS);
            } else {
                SystemProperties.set(PROPERTY_ODM_CCODE, "eu");
            }
        }
    }
}
+17 −0
Original line number Diff line number Diff line
type devicesettings_app, domain;

app_domain(devicesettings_app)

# Allow devicesettings_app to find *_service
allow devicesettings_app {
  activity_service
  content_capture_service
  netstats_service
  sensor_privacy_service
  thermal_service
  vibrator_manager_service
  wifi_service
}:service_manager find;

# Allow setting countrycode property
set_prop(devicesettings_app, system_fp_prop)
Loading