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

Commit b8912f54 authored by Robert Greenwalt's avatar Robert Greenwalt
Browse files

Perist Throttle data across reboots

Never got SharedPreferences working for this system service.  Didn't really need it anyway.
Using a straight up file now.

bug:2576057
Change-Id: Idad926e8242a85e22718c25f3f6c03e5749badac
parent d3bb93f6
Loading
Loading
Loading
Loading
+67 −21
Original line number Original line Diff line number Diff line
@@ -28,11 +28,11 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.content.res.Resources;
import android.content.SharedPreferences;
import android.database.ContentObserver;
import android.database.ContentObserver;
import android.net.IThrottleManager;
import android.net.IThrottleManager;
import android.net.ThrottleManager;
import android.net.ThrottleManager;
import android.os.Binder;
import android.os.Binder;
import android.os.Environment;
import android.os.Handler;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.IBinder;
@@ -48,7 +48,12 @@ import android.util.Slog;


import com.android.internal.telephony.TelephonyProperties;
import com.android.internal.telephony.TelephonyProperties;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.GregorianCalendar;
@@ -353,6 +358,8 @@ public class ThrottleService extends IThrottleManager.Stub {


            onResetAlarm();
            onResetAlarm();


            onPollAlarm();

            Intent broadcast = new Intent(ThrottleManager.POLICY_CHANGED_ACTION);
            Intent broadcast = new Intent(ThrottleManager.POLICY_CHANGED_ACTION);
            mContext.sendBroadcast(broadcast);
            mContext.sendBroadcast(broadcast);
        }
        }
@@ -591,7 +598,6 @@ public class ThrottleService extends IThrottleManager.Stub {


        ThrottleService mParent;
        ThrottleService mParent;
        Context mContext;
        Context mContext;
        SharedPreferences mSharedPreferences;


        DataRecorder(Context context, ThrottleService parent) {
        DataRecorder(Context context, ThrottleService parent) {
            mContext = context;
            mContext = context;
@@ -605,9 +611,6 @@ public class ThrottleService extends IThrottleManager.Stub {
                mPeriodStart = Calendar.getInstance();
                mPeriodStart = Calendar.getInstance();
                mPeriodEnd = Calendar.getInstance();
                mPeriodEnd = Calendar.getInstance();


                mSharedPreferences = mContext.getSharedPreferences("ThrottleData",
                        android.content.Context.MODE_PRIVATE);

                zeroData(0);
                zeroData(0);
                retrieve();
                retrieve();
            }
            }
@@ -698,17 +701,28 @@ public class ThrottleService extends IThrottleManager.Stub {
            record();
            record();
        }
        }


        private void record() {
        private File getDataFile() {
            // serialize into a secure setting
            File dataDir = Environment.getDataDirectory();
            File throttleDir = new File(dataDir, "system/throttle");
            throttleDir.mkdirs();
            File dataFile = new File(throttleDir, "data");
            return dataFile;
        }

        private static final int DATA_FILE_VERSION = 1;


        private void record() {
            // 1 int version
            // 1 int mPeriodCount
            // 1 int mPeriodCount
            // 13*6 long[PERIOD_COUNT] mPeriodRxData
            // 13*6 long[PERIOD_COUNT] mPeriodRxData
            // 13*6 long[PERIOD_COUNT] mPeriodTxData
            // 13*6 long[PERIOD_COUNT] mPeriodTxData
            // 1  int mCurrentPeriod
            // 1  int mCurrentPeriod
            // 13 long periodStartMS
            // 13 long periodStartMS
            // 13 long periodEndMS
            // 13 long periodEndMS
            // 199 chars max
            // 200 chars max
            StringBuilder builder = new StringBuilder();
            StringBuilder builder = new StringBuilder();
            builder.append(DATA_FILE_VERSION);
            builder.append(":");
            builder.append(mPeriodCount);
            builder.append(mPeriodCount);
            builder.append(":");
            builder.append(":");
            for(int i = 0; i < mPeriodCount; i++) {
            for(int i = 0; i < mPeriodCount; i++) {
@@ -726,22 +740,54 @@ public class ThrottleService extends IThrottleManager.Stub {
            builder.append(mPeriodEnd.getTimeInMillis());
            builder.append(mPeriodEnd.getTimeInMillis());
            builder.append(":");
            builder.append(":");


            SharedPreferences.Editor editor = mSharedPreferences.edit();
            BufferedWriter out = null;

            try {
            editor.putString("Data", builder.toString());
                out = new BufferedWriter(new FileWriter(getDataFile()),256);
            editor.commit();
                out.write(builder.toString());
            } catch (IOException e) {
                Slog.e(TAG, "Error writing data file");
                return;
            } finally {
                if (out != null) {
                    try {
                        out.close();
                    } catch (Exception e) {}
                }
            }
        }
        }


        private void retrieve() {
        private void retrieve() {
            String data = mSharedPreferences.getString("Data", "");
            File f = getDataFile();
//            String data = Settings.Secure.getString(mContext.getContentResolver(),
            byte[] buffer;
//                    Settings.Secure.THROTTLE_VALUE);
            FileInputStream s = null;
            try {
                buffer = new byte[(int)f.length()];
                s = new FileInputStream(f);
                s.read(buffer);
            } catch (IOException e) {
                Slog.e(TAG, "Error reading data file");
                return;
            } finally {
                if (s != null) {
                    try {
                        s.close();
                    } catch (Exception e) {}
                }
            }
            String data = new String(buffer);
            if (data == null || data.length() == 0) return;
            if (data == null || data.length() == 0) return;

            synchronized (mParent) {
            synchronized (mParent) {
                String[] parsed = data.split(":");
                String[] parsed = data.split(":");
                int parsedUsed = 0;
                int parsedUsed = 0;
                if (parsed.length < 6) return;
                if (parsed.length < 6) {
                    Slog.e(TAG, "reading data file with insufficient length - ignoring");
                    return;
                }

                if (Integer.parseInt(parsed[parsedUsed++]) != DATA_FILE_VERSION) {
                    Slog.e(TAG, "reading data file with bad version - ignoring");
                    return;
                }


                mPeriodCount = Integer.parseInt(parsed[parsedUsed++]);
                mPeriodCount = Integer.parseInt(parsed[parsedUsed++]);
                if (parsed.length != 4 + (2 * mPeriodCount)) return;
                if (parsed.length != 4 + (2 * mPeriodCount)) return;