Search in sources :

Example 1 with BatteryInfoUnavailableException

use of com.asksven.android.common.privateapiproxies.BatteryInfoUnavailableException in project BetterBatteryStats by asksven.

the class StatsProvider method getCurrentOtherUsageStatList.

public ArrayList<StatElement> getCurrentOtherUsageStatList(boolean bFilter, boolean bFilterView, boolean bWidget) throws Exception {
    Context ctx = BbsApplication.getAppContext();
    ArrayList<StatElement> myStats = new ArrayList<StatElement>();
    // List to store the other usages to
    ArrayList<StatElement> myUsages = new ArrayList<StatElement>();
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    boolean permsNotNeeded = sharedPrefs.getBoolean("ignore_system_app", false);
    if (!SysUtils.hasBatteryStatsPermission(ctx) && !permsNotNeeded) {
        long elapsedRealtime = SystemClock.elapsedRealtime();
        long uptimeMillis = SystemClock.uptimeMillis();
        // retrieve screen on time from prefs
        long screenOnTime = sharedPrefs.getLong("screen_on_counter", 0);
        myUsages.add(new Misc("Screen On", screenOnTime, elapsedRealtime));
        // basic fonctionality
        // elapsedRealtime(): Returns milliseconds since boot, including time spent in sleep.
        // uptimeMillis(): Returns milliseconds since boot, not counting time spent in deep sleep.
        Misc deepSleepUsage = new Misc("Deep Sleep", elapsedRealtime - uptimeMillis, elapsedRealtime);
        myUsages.add(deepSleepUsage);
        Misc awakeUsage = new Misc(LABEL_MISC_AWAKE, uptimeMillis, elapsedRealtime);
        myUsages.add(awakeUsage);
    } else {
        BatteryStatsProxy mStats = BatteryStatsProxy.getInstance(ctx);
        long rawRealtime = SystemClock.elapsedRealtime() * 1000;
        long uptime = SystemClock.uptimeMillis();
        long elaspedRealtime = rawRealtime / 1000;
        long batteryRealtime = 0;
        try {
            batteryRealtime = mStats.getBatteryRealtime(rawRealtime);
        } catch (Exception e) {
            Log.e(TAG, "An exception occured processing battery realtime. Message: " + e.getMessage());
            Log.e(TAG, "Exception: " + Log.getStackTraceString(e));
        }
        int statsType = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            statsType = BatteryStatsTypesLolipop.STATS_CURRENT;
        } else {
            statsType = BatteryStatsTypes.STATS_CURRENT;
        }
        long whichRealtime = mStats.computeBatteryRealtime(rawRealtime, statsType) / 1000;
        long timeBatteryUp = mStats.computeBatteryUptime(SystemClock.uptimeMillis() * 1000, statsType) / 1000;
        if (CommonLogSettings.DEBUG) {
            Log.i(TAG, "whichRealtime = " + whichRealtime + " batteryRealtime = " + batteryRealtime + " timeBatteryUp=" + timeBatteryUp);
        }
        long timeScreenOn = mStats.getScreenOnTime(batteryRealtime, statsType) / 1000;
        long timePhoneOn = mStats.getPhoneOnTime(batteryRealtime, statsType) / 1000;
        long timeWifiOn = 0;
        long timeWifiRunning = 0;
        if (sharedPrefs.getBoolean("show_other_wifi", true) && !bWidget) {
            try {
                timeWifiOn = mStats.getWifiOnTime(batteryRealtime, statsType) / 1000;
                timeWifiRunning = mStats.getGlobalWifiRunningTime(batteryRealtime, statsType) / 1000;
            // long timeWifiMulticast =
            // mStats.getWifiMulticastTime(m_context, batteryRealtime,
            // BatteryStatsTypes.STATS_CURRENT) / 1000;
            // long timeWifiLocked = mStats.getFullWifiLockTime(m_context,
            // batteryRealtime, BatteryStatsTypes.STATS_CURRENT) / 1000;
            // long timeWifiScan = mStats.getScanWifiLockTime(m_context,
            // batteryRealtime, BatteryStatsTypes.STATS_CURRENT) / 1000;
            } catch (BatteryInfoUnavailableException e) {
                timeWifiOn = 0;
                timeWifiRunning = 0;
                Log.e(TAG, "A batteryinfo error occured while retrieving Wifi data");
            }
        }
        // long timeAudioOn = mStats.getAudioTurnedOnTime(m_context,
        // batteryRealtime, BatteryStatsTypes.STATS_CURRENT) / 1000;
        // long timeVideoOn = mStats.getVideoTurnedOnTime(m_context,
        // batteryRealtime, BatteryStatsTypes.STATS_CURRENT) / 1000;
        long timeBluetoothOn = 0;
        if (sharedPrefs.getBoolean("show_other_bt", true) && !bWidget) {
            try {
                if (Build.VERSION.SDK_INT >= 21) {
                    timeBluetoothOn = mStats.getBluetoothInStateTime(ctx, statsType) / 1000;
                } else {
                    timeBluetoothOn = mStats.getBluetoothOnTime(batteryRealtime, statsType) / 1000;
                }
            } catch (BatteryInfoUnavailableException e) {
                timeBluetoothOn = 0;
                Log.e(TAG, "A batteryinfo error occured while retrieving BT data");
            }
        }
        long interactiveTime = 0;
        long powerSaveModeEnabledTime = 0;
        long deviceIdleModeEnabledTime = 0;
        long getDeviceIdlingTime = 0;
        if (sharedPrefs.getBoolean("show_other_doze", true) && !bWidget) {
            try {
                if (Build.VERSION.SDK_INT >= 21) {
                    interactiveTime = mStats.getInteractiveTime(batteryRealtime, statsType) / 1000;
                    powerSaveModeEnabledTime = mStats.getPowerSaveModeEnabledTime(batteryRealtime, statsType) / 1000;
                    deviceIdleModeEnabledTime = mStats.getDeviceIdleModeEnabledTime(batteryRealtime, statsType) / 1000;
                    // these are not available anymore from SDK24 on
                    if (Build.VERSION.SDK_INT <= 23) {
                        getDeviceIdlingTime = mStats.getDeviceIdlingTime(batteryRealtime, statsType) / 1000;
                    } else {
                    // we need to switch to getDeviceIdleModeTime
                    }
                }
            } catch (BatteryInfoUnavailableException e) {
                timeBluetoothOn = 0;
                Log.e(TAG, "A batteryinfo error occured while retrieving doze mode data");
            }
        }
        long syncTime = 0;
        try {
            if (Build.VERSION.SDK_INT >= 21) {
                syncTime = mStats.getSyncOnTime(ctx, batteryRealtime, statsType) / 1000;
            }
        } catch (BatteryInfoUnavailableException e) {
            Log.e(TAG, "A batteryinfo error occured while retrieving sensor and sync stats");
        }
        long timeNoDataConnection = 0;
        long timeSignalNone = 0;
        long timeSignalPoor = 0;
        long timeSignalModerate = 0;
        long timeSignalGood = 0;
        long timeSignalGreat = 0;
        if (sharedPrefs.getBoolean("show_other_signal", true)) {
            try {
                timeNoDataConnection = mStats.getPhoneDataConnectionTime(BatteryStatsTypes.DATA_CONNECTION_NONE, batteryRealtime, statsType) / 1000;
                timeSignalNone = mStats.getPhoneSignalStrengthTime(BatteryStatsTypes.SIGNAL_STRENGTH_NONE_OR_UNKNOWN, batteryRealtime, statsType) / 1000;
                timeSignalPoor = mStats.getPhoneSignalStrengthTime(BatteryStatsTypes.SIGNAL_STRENGTH_POOR, batteryRealtime, statsType) / 1000;
                timeSignalModerate = mStats.getPhoneSignalStrengthTime(BatteryStatsTypes.SIGNAL_STRENGTH_MODERATE, batteryRealtime, statsType) / 1000;
                timeSignalGood = mStats.getPhoneSignalStrengthTime(BatteryStatsTypes.SIGNAL_STRENGTH_GOOD, batteryRealtime, statsType) / 1000;
                timeSignalGreat = mStats.getPhoneSignalStrengthTime(BatteryStatsTypes.SIGNAL_STRENGTH_GREAT, batteryRealtime, statsType) / 1000;
            } catch (BatteryInfoUnavailableException e) {
                timeNoDataConnection = 0;
                timeSignalNone = 0;
                timeSignalPoor = 0;
                timeSignalModerate = 0;
                timeSignalGood = 0;
                timeSignalGreat = 0;
                Log.e(TAG, "A batteryinfo error occured while retrieving Signal data");
            }
        }
        long timeScreenDark = 0;
        long timeScreenDim = 0;
        long timeScreenMedium = 0;
        long timeScreenLight = 0;
        long timeScreenBright = 0;
        if (sharedPrefs.getBoolean("show_other_screen_brightness", true)) {
            try {
                timeScreenDark = mStats.getScreenBrightnessTime(BatteryStatsTypes.SCREEN_BRIGHTNESS_DARK, batteryRealtime, statsType) / 1000;
                timeScreenDim = mStats.getScreenBrightnessTime(BatteryStatsTypes.SCREEN_BRIGHTNESS_DIM, batteryRealtime, statsType) / 1000;
                timeScreenMedium = mStats.getScreenBrightnessTime(BatteryStatsTypes.SCREEN_BRIGHTNESS_MEDIUM, batteryRealtime, statsType) / 1000;
                timeScreenLight = mStats.getScreenBrightnessTime(BatteryStatsTypes.SCREEN_BRIGHTNESS_LIGHT, batteryRealtime, statsType) / 1000;
                timeScreenBright = mStats.getScreenBrightnessTime(BatteryStatsTypes.SCREEN_BRIGHTNESS_BRIGHT, batteryRealtime, statsType) / 1000;
            } catch (BatteryInfoUnavailableException e) {
                timeScreenDark = 0;
                timeScreenDim = 0;
                timeScreenMedium = 0;
                timeScreenLight = 0;
                timeScreenBright = 0;
                Log.e(TAG, "A batteryinfo error occured while retrieving Screen brightness data");
            }
        }
        // deep sleep times are independent of stat type
        long timeDeepSleep = (SystemClock.elapsedRealtime() - SystemClock.uptimeMillis());
        // long whichRealtime = SystemClock.elapsedRealtime();
        // long timeElapsed = mStats.computeBatteryRealtime(rawRealtime,
        // BatteryStatsTypes.STATS_CURRENT) / 1000;
        // SystemClock.elapsedRealtime();
        Misc deepSleepUsage = new Misc("Deep Sleep", timeDeepSleep, elaspedRealtime);
        if (LogSettings.DEBUG) {
            Log.d(TAG, "Added Deep sleep:" + deepSleepUsage.toString());
        }
        if ((!bFilter) || (deepSleepUsage.getTimeOn() > 0)) {
            myUsages.add(deepSleepUsage);
        }
        if (timeBatteryUp > 0) {
            myUsages.add(new Misc(LABEL_MISC_AWAKE, timeBatteryUp - timeScreenOn, elaspedRealtime));
        }
        if (timeScreenOn > 0) {
            myUsages.add(new Misc("Screen On", timeScreenOn, elaspedRealtime));
        }
        if (timePhoneOn > 0) {
            myUsages.add(new Misc("Phone On", timePhoneOn, elaspedRealtime));
        }
        if ((timeWifiOn > 0) && (!bFilterView || sharedPrefs.getBoolean("show_other_wifi", true))) {
            myUsages.add(new Misc("Wifi On", timeWifiOn, elaspedRealtime));
        }
        if ((timeWifiRunning > 0) && (!bFilterView || sharedPrefs.getBoolean("show_other_wifi", true))) {
            myUsages.add(new Misc("Wifi Running", timeWifiRunning, elaspedRealtime));
        }
        if ((timeBluetoothOn > 0) && (!bFilterView || sharedPrefs.getBoolean("show_other_bt", true))) {
            myUsages.add(new Misc("Bluetooth On", timeBluetoothOn, elaspedRealtime));
        }
        if (Build.VERSION.SDK_INT >= 6) {
            if ((interactiveTime > 0) && (!bFilterView || sharedPrefs.getBoolean("show_other_doze", true))) {
                myUsages.add(new Misc("Doze Interactive Time", interactiveTime, elaspedRealtime));
            }
            if ((powerSaveModeEnabledTime > 0) && (!bFilterView || sharedPrefs.getBoolean("show_other_doze", true))) {
                myUsages.add(new Misc("Doze Powersave Time", powerSaveModeEnabledTime, elaspedRealtime));
            }
            if ((deviceIdleModeEnabledTime > 0) && (!bFilterView || sharedPrefs.getBoolean("show_other_doze", true))) {
                myUsages.add(new Misc("Doze Idle Mode Time", deviceIdleModeEnabledTime, elaspedRealtime));
            }
            if ((getDeviceIdlingTime > 0) && (!bFilterView || sharedPrefs.getBoolean("show_other_doze", true))) {
                myUsages.add(new Misc("Doze Idling Time", getDeviceIdlingTime, elaspedRealtime));
            }
            if (syncTime > 0) {
                myUsages.add(new Misc("Sync", syncTime, elaspedRealtime));
            }
        }
        if ((timeNoDataConnection > 0) && (!bFilterView || sharedPrefs.getBoolean("show_other_connection", true))) {
            myUsages.add(new Misc("No Data Connection", timeNoDataConnection, elaspedRealtime));
        }
        if ((timeSignalNone > 0) && (!bFilterView || sharedPrefs.getBoolean("show_other_signal", true))) {
            myUsages.add(new Misc("No or Unknown Signal", timeSignalNone, elaspedRealtime));
        }
        if ((timeSignalPoor > 0) && (!bFilterView || sharedPrefs.getBoolean("show_other_signal", true))) {
            myUsages.add(new Misc("Poor Signal", timeSignalPoor, elaspedRealtime));
        }
        if ((timeSignalModerate > 0) && (!bFilterView || sharedPrefs.getBoolean("show_other_signal", true))) {
            myUsages.add(new Misc("Moderate Signal", timeSignalModerate, elaspedRealtime));
        }
        if ((timeSignalGood > 0) && (!bFilterView || sharedPrefs.getBoolean("show_other_signal", true))) {
            myUsages.add(new Misc("Good Signal", timeSignalGood, elaspedRealtime));
        }
        if ((timeSignalGreat > 0) && (!bFilterView || sharedPrefs.getBoolean("show_other_signal", true))) {
            myUsages.add(new Misc("Great Signal", timeSignalGreat, elaspedRealtime));
        }
        if ((timeScreenDark > 0) && (!bFilterView || sharedPrefs.getBoolean("show_other_screen_brightness", true))) {
            myUsages.add(new Misc("Screen dark", timeScreenDark, elaspedRealtime));
        }
        if ((timeScreenDim > 0) && (!bFilterView || sharedPrefs.getBoolean("show_other_screen_brightness", true))) {
            myUsages.add(new Misc("Screen dimmed", timeScreenDim, elaspedRealtime));
        }
        if ((timeScreenMedium > 0) && (!bFilterView || sharedPrefs.getBoolean("show_other_screen_brightness", true))) {
            myUsages.add(new Misc("Screen medium", timeScreenMedium, elaspedRealtime));
        }
        if ((timeScreenLight > 0) && (!bFilterView || sharedPrefs.getBoolean("show_other_screen_brightness", true))) {
            myUsages.add(new Misc("Screen light", timeScreenLight, elaspedRealtime));
        }
        if ((timeScreenBright > 0) && (!bFilterView || sharedPrefs.getBoolean("show_other_screen_brightness", true))) {
            myUsages.add(new Misc("Screen bright", timeScreenBright, elaspedRealtime));
        }
    // if ( (timeWifiMulticast > 0) && (!bFilterView ||
    // sharedPrefs.getBoolean("show_other_wifi", true)) )
    // {
    // myUsages.add(new Misc("Wifi Multicast On", timeWifiMulticast,
    // elaspedRealtme));
    // }
    // 
    // if ( (timeWifiLocked > 0) && (!bFilterView ||(!bFilterView ||
    // sharedPrefs.getBoolean("show_other_wifi", true)) )
    // {
    // myUsages.add(new Misc("Wifi Locked", timeWifiLocked, elaspedRealtme));
    // }
    // 
    // if ( (timeWifiScan > 0) && (!bFilterView ||
    // sharedPrefs.getBoolean("show_other_wifi", true)) )
    // {
    // myUsages.add(new Misc("Wifi Scan", timeWifiScan, elaspedRealtme));
    // }
    // 
    // if (timeAudioOn > 0)
    // {
    // myUsages.add(new Misc("Video On", timeAudioOn, elaspedRealtme));
    // }
    // 
    // if (timeVideoOn > 0)
    // {
    // myUsages.add(new Misc("Video On", timeVideoOn, elaspedRealtme));
    // }
    // sort @see
    // com.asksven.android.common.privateapiproxies.Walkelock.compareTo
    // Collections.sort(myUsages);
    }
    for (int i = 0; i < myUsages.size(); i++) {
        Misc usage = (Misc) myUsages.get(i);
        if (LogSettings.DEBUG) {
            Log.d(TAG, "Current value: " + usage.getName() + " " + usage.toString());
        }
        if ((!bFilter) || (usage.getTimeOn() > 0)) {
            myStats.add((StatElement) usage);
        }
    }
    return myStats;
}
Also used : Context(android.content.Context) SharedPreferences(android.content.SharedPreferences) StatElement(com.asksven.android.common.privateapiproxies.StatElement) ArrayList(java.util.ArrayList) Misc(com.asksven.android.common.privateapiproxies.Misc) BatteryInfoUnavailableException(com.asksven.android.common.privateapiproxies.BatteryInfoUnavailableException) BatteryStatsProxy(com.asksven.android.common.privateapiproxies.BatteryStatsProxy) ReceiverCallNotAllowedException(android.content.ReceiverCallNotAllowedException) BatteryInfoUnavailableException(com.asksven.android.common.privateapiproxies.BatteryInfoUnavailableException) SuppressLint(android.annotation.SuppressLint)

Example 2 with BatteryInfoUnavailableException

use of com.asksven.android.common.privateapiproxies.BatteryInfoUnavailableException in project BetterBatteryStats by asksven.

the class StatsActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    super.onCreate(savedInstanceState);
    // HockeyApp
    try {
        MetricsManager.register(getApplication());
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
    // Log.i(TAG, "OnCreated called");
    setContentView(R.layout.stats);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle(getString(R.string.app_name));
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayUseLogoEnabled(false);
    // set debugging
    if (sharedPrefs.getBoolean("debug_logging", false)) {
        LogSettings.DEBUG = true;
        CommonLogSettings.DEBUG = true;
    } else {
        LogSettings.DEBUG = false;
        CommonLogSettings.DEBUG = false;
    }
    swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);
    swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            doRefresh(true);
        }
    });
    // /////////////////////////////////////////////
    // check if we have a new release
    // /////////////////////////////////////////////
    // if yes do some migration (if required) and show release notes
    String strLastRelease = sharedPrefs.getString("last_release", "0");
    String strCurrentRelease = "";
    try {
        PackageInfo pinfo = getPackageManager().getPackageInfo(getPackageName(), 0);
        strCurrentRelease = Integer.toString(pinfo.versionCode);
    } catch (Exception e) {
    // nop strCurrentRelease is set to ""
    }
    // if root is available use it
    boolean ignoreSystemApp = sharedPrefs.getBoolean("ignore_system_app", false);
    // Grant permissions if they are missing and root is available
    if (!SysUtils.hasBatteryStatsPermission(this) || !SysUtils.hasDumpsysPermission(this) || !SysUtils.hasPackageUsageStatsPermission(this)) {
        if ((!ignoreSystemApp && RootShell.getInstance().hasRootPermissions())) {
            // attempt to set perms using pm-comand
            Log.i(TAG, "attempting to grant perms with 'pm grant'");
            String pkg = this.getPackageName();
            RootShell.getInstance().run("pm grant " + pkg + " android.permission.BATTERY_STATS");
            RootShell.getInstance().run("pm grant " + pkg + " android.permission.DUMP");
            RootShell.getInstance().run("pm grant " + pkg + " android.permission.PACKAGE_USAGE_STATS");
            if (SysUtils.hasBatteryStatsPermission(this)) {
                Log.i(TAG, "succeeded");
            } else {
                Log.i(TAG, "failed");
            }
        }
    }
    // Package usage stats were introduced in SDK21 so we need to make the distinction
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        // show install as system app screen if root available but perms missing
        if (!SysUtils.hasBatteryStatsPermission(this) || !SysUtils.hasDumpsysPermission(this) || !SysUtils.hasPackageUsageStatsPermission(this)) {
            Intent intentSystemApp = new Intent(this, SystemAppActivity.class);
            this.startActivity(intentSystemApp);
        }
    } else {
        if (!SysUtils.hasBatteryStatsPermission(this) || !SysUtils.hasDumpsysPermission(this)) {
            Intent intentSystemApp = new Intent(this, SystemAppActivity.class);
            this.startActivity(intentSystemApp);
        }
    }
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    // first start
    if (strLastRelease.equals("0")) {
        boolean firstLaunch = !prefs.getBoolean("launched", false);
        if (firstLaunch) {
            // Save that the app has been launched
            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("launched", true);
            editor.commit();
            // start service to persist reference
            Intent serviceIntent = new Intent(this, WriteUnpluggedReferenceService.class);
            this.startService(serviceIntent);
            // refresh widgets
            Intent intentRefreshWidgets = new Intent(LargeWidgetProvider.WIDGET_UPDATE);
            this.sendBroadcast(intentRefreshWidgets);
        }
        SharedPreferences.Editor updater = sharedPrefs.edit();
        updater.putString("last_release", strCurrentRelease);
        updater.commit();
    } else if (!strLastRelease.equals(strCurrentRelease)) {
        // save the current release to properties so that the dialog won't be shown till next version
        SharedPreferences.Editor updater = sharedPrefs.edit();
        updater.putString("last_release", strCurrentRelease);
        updater.commit();
        // we don't need to delete refs as long as we don't change the database schema
        // Toast.makeText(this, getString(R.string.info_deleting_refs), Toast.LENGTH_SHORT).show();
        // ReferenceStore.deleteAllRefs(this);
        // Intent i = new Intent(this, WriteBootReferenceService.class);
        // this.startService(i);
        // i = new Intent(this, WriteUnpluggedReferenceService.class);
        // this.startService(i);
        ChangeLog cl = new ChangeLog(this);
        cl.getLogDialog().show();
    }
    // /////////////////////////////////////////////
    // retrieve default selections for spinners
    // if none were passed
    // /////////////////////////////////////////////
    m_iStat = Integer.valueOf(sharedPrefs.getString("default_stat", "0"));
    m_refFromName = sharedPrefs.getString("default_stat_type", Reference.UNPLUGGED_REF_FILENAME);
    if (!ReferenceStore.hasReferenceByName(m_refFromName, this)) {
        m_refFromName = Reference.BOOT_REF_FILENAME;
        Toast.makeText(this, getString(R.string.info_fallback_to_boot), Toast.LENGTH_SHORT).show();
    }
    if (LogSettings.DEBUG)
        Log.i(TAG, "onCreate state from preferences: refFrom=" + m_refFromName + " refTo=" + m_refToName);
    try {
        // recover any saved state
        if ((savedInstanceState != null) && (!savedInstanceState.isEmpty())) {
            m_iStat = (Integer) savedInstanceState.getSerializable("stat");
            m_refFromName = (String) savedInstanceState.getSerializable("stattypeFrom");
            m_refToName = (String) savedInstanceState.getSerializable("stattypeTo");
            if (LogSettings.DEBUG)
                Log.i(TAG, "onCreate retrieved saved state: refFrom=" + m_refFromName + " refTo=" + m_refToName);
        }
    } catch (Exception e) {
        m_iStat = Integer.valueOf(sharedPrefs.getString("default_stat", "0"));
        m_refFromName = sharedPrefs.getString("default_stat_type", Reference.UNPLUGGED_REF_FILENAME);
        Log.e(TAG, "Exception: " + e.getMessage());
        DataStorage.LogToFile(LOGFILE, "Exception in onCreate restoring Bundle");
        DataStorage.LogToFile(LOGFILE, e.getMessage());
        DataStorage.LogToFile(LOGFILE, e.getStackTrace());
        Toast.makeText(this, getString(R.string.info_state_recovery_error), Toast.LENGTH_SHORT).show();
    }
    // Handle the case the Activity was called from an intent with paramaters
    Bundle extras = getIntent().getExtras();
    if ((extras != null) && !extras.isEmpty()) {
        // Override if some values were passed to the intent
        if (extras.containsKey(StatsActivity.STAT))
            m_iStat = extras.getInt(StatsActivity.STAT);
        if (extras.containsKey(StatsActivity.STAT_TYPE_FROM))
            m_refFromName = extras.getString(StatsActivity.STAT_TYPE_FROM);
        if (extras.containsKey(StatsActivity.STAT_TYPE_TO))
            m_refToName = extras.getString(StatsActivity.STAT_TYPE_TO);
        if (LogSettings.DEBUG)
            Log.i(TAG, "onCreate state from extra: refFrom=" + m_refFromName + " refTo=" + m_refToName);
        boolean bCalledFromNotification = extras.getBoolean(StatsActivity.FROM_NOTIFICATION, false);
        // Clear the notifications that was clicked to call the activity
        if (bCalledFromNotification) {
            NotificationManager nM = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
            nM.cancel(EventWatcherService.NOTFICATION_ID);
        }
    }
    // Spinner for selecting the stat
    Spinner spinnerStat = (Spinner) findViewById(R.id.spinnerStat);
    ArrayAdapter spinnerStatAdapter = ArrayAdapter.createFromResource(this, R.array.stats, // android.R.layout.simple_spinner_item);
    R.layout.bbs_spinner_layout);
    // android.R.layout.simple_spinner_dropdown_item);
    spinnerStatAdapter.setDropDownViewResource(R.layout.bbs_spinner_dropdown_item);
    spinnerStat.setAdapter(spinnerStatAdapter);
    // setSelection MUST be called after setAdapter
    spinnerStat.setSelection(m_iStat);
    spinnerStat.setOnItemSelectedListener(this);
    // /////////////////////////////////////////////
    // Spinner for Selecting the Stat type
    // /////////////////////////////////////////////
    Spinner spinnerStatType = (Spinner) findViewById(R.id.spinnerStatType);
    // android.R.layout.simple_spinner_item);
    m_spinnerFromAdapter = new ReferencesAdapter(this, R.layout.bbs_spinner_layout);
    // android.R.layout.simple_spinner_dropdown_item);
    m_spinnerFromAdapter.setDropDownViewResource(R.layout.bbs_spinner_dropdown_item);
    spinnerStatType.setAdapter(m_spinnerFromAdapter);
    try {
        this.setListViewAdapter();
    } catch (BatteryInfoUnavailableException e) {
        Log.e(TAG, "Exception: " + Log.getStackTraceString(e));
        Snackbar.make(findViewById(android.R.id.content), R.string.info_service_connection_error, Snackbar.LENGTH_LONG).show();
    // Toast.makeText(this,
    // getString(R.string.info_service_connection_error),
    // Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        // Log.e(TAG, e.getMessage(), e.fillInStackTrace());
        Log.e(TAG, "Exception: " + Log.getStackTraceString(e));
        Toast.makeText(this, getString(R.string.info_unknown_stat_error), Toast.LENGTH_LONG).show();
    }
    // setSelection MUST be called after setAdapter
    spinnerStatType.setSelection(m_spinnerFromAdapter.getPosition(m_refFromName));
    spinnerStatType.setOnItemSelectedListener(this);
    // /////////////////////////////////////////////
    // Spinner for Selecting the end sample
    // /////////////////////////////////////////////
    Spinner spinnerStatSampleEnd = (Spinner) findViewById(R.id.spinnerStatSampleEnd);
    // android.R.layout.simple_spinner_item);
    m_spinnerToAdapter = new ReferencesAdapter(this, R.layout.bbs_spinner_layout);
    // android.R.layout.simple_spinner_dropdown_item);
    m_spinnerToAdapter.setDropDownViewResource(R.layout.bbs_spinner_dropdown_item);
    spinnerStatSampleEnd.setVisibility(View.VISIBLE);
    spinnerStatSampleEnd.setAdapter(m_spinnerToAdapter);
    // setSelection must be called after setAdapter
    if ((m_refToName != null) && !m_refToName.equals("")) {
        int pos = m_spinnerToAdapter.getPosition(m_refToName);
        spinnerStatSampleEnd.setSelection(pos);
    } else {
        spinnerStatSampleEnd.setSelection(m_spinnerToAdapter.getPosition(Reference.CURRENT_REF_FILENAME));
    }
    spinnerStatSampleEnd.setOnItemSelectedListener(this);
    // /////////////////////////////////////////////
    // sorting
    // /////////////////////////////////////////////
    m_iSorting = 0;
    // log reference store
    ReferenceStore.logReferences(this);
    if (LogSettings.DEBUG) {
        Log.i(TAG, "onCreate final state: refFrom=" + m_refFromName + " refTo=" + m_refToName);
        Log.i(TAG, "OnCreated end");
    }
}
Also used : NotificationManager(android.app.NotificationManager) SharedPreferences(android.content.SharedPreferences) PackageInfo(android.content.pm.PackageInfo) Bundle(android.os.Bundle) Spinner(android.widget.Spinner) Intent(android.content.Intent) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) BatteryInfoUnavailableException(com.asksven.android.common.privateapiproxies.BatteryInfoUnavailableException) ReferencesAdapter(com.asksven.betterbatterystats.adapters.ReferencesAdapter) ChangeLog(de.cketti.library.changelog.ChangeLog) BatteryInfoUnavailableException(com.asksven.android.common.privateapiproxies.BatteryInfoUnavailableException) ArrayAdapter(android.widget.ArrayAdapter) Toolbar(android.support.v7.widget.Toolbar)

Aggregations

SharedPreferences (android.content.SharedPreferences)2 BatteryInfoUnavailableException (com.asksven.android.common.privateapiproxies.BatteryInfoUnavailableException)2 SuppressLint (android.annotation.SuppressLint)1 NotificationManager (android.app.NotificationManager)1 Context (android.content.Context)1 Intent (android.content.Intent)1 ReceiverCallNotAllowedException (android.content.ReceiverCallNotAllowedException)1 PackageInfo (android.content.pm.PackageInfo)1 Bundle (android.os.Bundle)1 SwipeRefreshLayout (android.support.v4.widget.SwipeRefreshLayout)1 Toolbar (android.support.v7.widget.Toolbar)1 ArrayAdapter (android.widget.ArrayAdapter)1 Spinner (android.widget.Spinner)1 BatteryStatsProxy (com.asksven.android.common.privateapiproxies.BatteryStatsProxy)1 Misc (com.asksven.android.common.privateapiproxies.Misc)1 StatElement (com.asksven.android.common.privateapiproxies.StatElement)1 ReferencesAdapter (com.asksven.betterbatterystats.adapters.ReferencesAdapter)1 ChangeLog (de.cketti.library.changelog.ChangeLog)1 ArrayList (java.util.ArrayList)1