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

Commit 23a424b0 authored by Mohit Mali's avatar Mohit Mali Committed by Nishith Khanna
Browse files

Settings: Switch to select prod or staging OTA server

Settings: display alert dialog when switch ota server
parent 1c1eded7
Loading
Loading
Loading
Loading
+6 −0
Original line number Original line Diff line number Diff line
@@ -5483,6 +5483,12 @@
            </intent-filter>
            </intent-filter>
        </activity>
        </activity>


        <provider
            android:name="com.android.settings.development.OTAProvider"
            android:authorities="custom.setting.Provider.OTA_SERVER"
            android:exported="true"
            android:multiprocess="true" />

        <!-- This is the longest AndroidManifest.xml ever. -->
        <!-- This is the longest AndroidManifest.xml ever. -->
    </application>
    </application>
</manifest>
</manifest>
+6 −0
Original line number Original line Diff line number Diff line
@@ -208,4 +208,10 @@


    <!-- /e/ updater -->
    <!-- /e/ updater -->
    <string name="system_update_summary">Updates, preferences, release notes</string>
    <string name="system_update_summary">Updates, preferences, release notes</string>

    <!-- /e/ OTA server change -->
    <string name="title_ota_server">Connect to /e/ OS test channel</string>
    <string name="use_ota_summary">Access test releases of /e/ OS</string>
    <string name="use_ota_staging_popup_title">You are about to connect to /e/ OS test channel</string>
    <string name="use_ota_staging_popup_message">It will allow you to download and install test versions of /e/ OS. Those builds may be unstable, and should be used only for test purposes. Please accept only if you know what you are doing.</string>
</resources>
</resources>
+5 −0
Original line number Original line Diff line number Diff line
@@ -790,6 +790,11 @@
            android:title="@string/enable_notes_role_title"
            android:title="@string/enable_notes_role_title"
            android:summary="@string/enable_notes_role_summary" />
            android:summary="@string/enable_notes_role_summary" />


        <SwitchPreference
            android:key="change_update_source"
            android:title="@string/title_ota_server"
            android:summary="@string/use_ota_summary" />

    </PreferenceCategory>
    </PreferenceCategory>


    <PreferenceCategory
    <PreferenceCategory
+138 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2021 ECORP SAS
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.android.settings.development;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.net.Uri;

import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.SwitchPreference;

import com.android.settings.R;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.development.DeveloperOptionsPreferenceController;


public class ChangeSourcePreferenceController extends DeveloperOptionsPreferenceController implements
        Preference.OnPreferenceChangeListener, PreferenceControllerMixin {

    @VisibleForTesting
    static final int SETTING_VALUE_ON = 1;
    @VisibleForTesting
    static final int SETTING_VALUE_OFF = 0;
    private static final String CHANGE_URL_KEY = "change_update_source";
    private final String UPDATE_URI = "content://custom.setting.Provider.OTA_SERVER/cte";
    private String status;

    public ChangeSourcePreferenceController(Context context) {
        super(context);
    }

    @Override
    public String getPreferenceKey() {
        return CHANGE_URL_KEY;
    }

    public void updateOtaServer() {
        if (count(OTAProvider.CONTENT_URI)) {
            ContentValues values = new ContentValues();

            if (retrieveStatus().equals("true")) {
                values.put(OTAProvider.Status, "false");
                mContext.getContentResolver().update(OTAProvider.CONTENT_URI, values, OTAProvider.id + "=?", new String[]{"1"});
            } else {
                values.put(OTAProvider.Status, "true");
                mContext.getContentResolver().update(OTAProvider.CONTENT_URI, values, OTAProvider.id + "=?", new String[]{"1"});
            }
        } else {
            ContentValues values = new ContentValues();
            values.put(OTAProvider.Status, "true");
            mContext.getContentResolver().insert(OTAProvider.CONTENT_URI, values);
        }
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        final boolean isEnabled = (Boolean) newValue;
        ((SwitchPreference) mPreference).setChecked(isEnabled);
        if (retrieveStatus().equals("true")) {
            updateOtaServer();
            return true;
        }
        new AlertDialog.Builder(mContext).
                setTitle(R.string.use_ota_staging_popup_title)
                .setMessage(mContext.getResources().getString(R.string.use_ota_staging_popup_message))
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        ((SwitchPreference) mPreference).setChecked(true);
                        updateOtaServer();
                    }
                }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        ((SwitchPreference) mPreference).setChecked(false);
                    }
                }).show();
        return false;
    }

    @Override
    public void updateState(Preference preference) {
        if (retrieveStatus().equals("true")) {
            ((SwitchPreference) mPreference).setChecked(true);
        } else {
            ((SwitchPreference) mPreference).setChecked(false);
        }
    }

    @Override
    protected void onDeveloperOptionsSwitchDisabled() {
        super.onDeveloperOptionsSwitchDisabled();
        ((SwitchPreference) mPreference).setChecked(false);
    }

    @Override
    protected void onDeveloperOptionsSwitchEnabled() {
        super.onDeveloperOptionsSwitchEnabled();
        if (!count(OTAProvider.CONTENT_URI)) {
            ContentValues values = new ContentValues();
            values.put(OTAProvider.Status, "false");
            mContext.getContentResolver().insert(OTAProvider.CONTENT_URI, values);
        }

    }

    public boolean count(Uri uri) {
        Cursor cursor = mContext.getContentResolver().query(uri, new String[]{"id"},
                null, null, null);
        return cursor.getCount() > 0;
    }

    private String retrieveStatus() {
        Cursor cursor = mContext.getContentResolver().query(Uri.parse(UPDATE_URI), null, OTAProvider.id + "=?", new String[]{"1"}, OTAProvider.Status);
        if (cursor.moveToFirst()) {
            do {
                status = cursor.getString(cursor.getColumnIndex(OTAProvider.Status));
            } while (cursor.moveToNext());
        }
        return status;
    }
}
+2 −0
Original line number Original line Diff line number Diff line
/*
/*
 * Copyright (C) 2017 The Android Open Source Project
 * Copyright (C) 2017 The Android Open Source Project
 * Copyright (C) 2021 ECORP SAS
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * you may not use this file except in compliance with the License.
@@ -765,6 +766,7 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
        controllers.add(new NfcSnoopLogPreferenceController(context, fragment));
        controllers.add(new NfcSnoopLogPreferenceController(context, fragment));
        controllers.add(new NfcVerboseVendorLogPreferenceController(context, fragment));
        controllers.add(new NfcVerboseVendorLogPreferenceController(context, fragment));
        controllers.add(new ShowTapsPreferenceController(context));
        controllers.add(new ShowTapsPreferenceController(context));
        controllers.add(new ChangeSourcePreferenceController(context));
        controllers.add(new PointerLocationPreferenceController(context));
        controllers.add(new PointerLocationPreferenceController(context));
        controllers.add(new ShowKeyPressesPreferenceController(context));
        controllers.add(new ShowKeyPressesPreferenceController(context));
        controllers.add(new TouchpadVisualizerPreferenceController(context));
        controllers.add(new TouchpadVisualizerPreferenceController(context));
Loading