Search in sources :

Example 1 with SensorUsage

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

the class StatsProvider method getSensorStatList.

/**
 * Get the Alarm Stat to be displayed
 *
 * @param bFilter
 *            defines if zero-values should be filtered out
 * @return a List of Other usages sorted by duration (descending)
 * @throws Exception
 *             if the API call failed
 */
public ArrayList<StatElement> getSensorStatList(boolean bFilter, Reference refFrom, Reference refTo) throws Exception {
    Context ctx = BbsApplication.getAppContext();
    ArrayList<StatElement> myStats = new ArrayList<StatElement>();
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    boolean permsNotNeeded = sharedPrefs.getBoolean("ignore_system_app", false);
    // to process alarms we need either root or the perms to access the private API
    if (!(permsNotNeeded || SysUtils.hasBatteryStatsPermission(ctx) || RootShell.getInstance().hasRootPermissions())) {
        myStats.add(new Notification(ctx.getString(R.string.NO_PERM_ERR)));
        return myStats;
    }
    if ((refFrom == null) || (refTo == null)) {
        myStats.add(new Notification(ctx.getString(R.string.NO_REF_ERR)));
        return myStats;
    }
    ArrayList<StatElement> mySensorStats = null;
    if ((refTo.m_refSensorUsage != null) && (!refTo.m_refSensorUsage.isEmpty())) {
        mySensorStats = refTo.m_refSensorUsage;
    } else {
        myStats.add(new Notification(ctx.getString(R.string.NO_STATS)));
        return myStats;
    }
    ArrayList<SensorUsage> myRetSensorStats = new ArrayList<SensorUsage>();
    // if we are using custom ref. always retrieve "stats current"
    // sort @see
    // com.asksven.android.common.privateapiproxies.Walkelock.compareTo
    String strCurrent = mySensorStats.toString();
    String strRef = "";
    String strRefDescr = "";
    if (LogSettings.DEBUG) {
        if (refFrom != null) {
            strRefDescr = refFrom.whoAmI();
            if (refFrom.m_refSensorUsage != null) {
                strRef = refFrom.m_refSensorUsage.toString();
            } else {
                strRef = "SensorUsage is null";
            }
        } else {
            strRefDescr = "Reference is null";
        }
        Log.d(TAG, "Processing sensor stats from " + refFrom.m_fileName + " to " + refTo.m_fileName);
        Log.d(TAG, "Reference used: " + strRefDescr);
        Log.d(TAG, "It is now " + DateUtils.now());
        Log.d(TAG, "Substracting " + strRef);
        Log.d(TAG, "from " + strCurrent);
    }
    for (int i = 0; i < mySensorStats.size(); i++) {
        SensorUsage sensor = ((SensorUsage) mySensorStats.get(i)).clone();
        if ((!bFilter) || ((sensor.getTotal()) > 0)) {
            sensor.substractFromRef(refFrom.m_refSensorUsage);
            // threshold
            if ((!bFilter) || ((sensor.getTotal()) > 0)) {
                myRetSensorStats.add(sensor);
            }
        }
    }
    Collections.sort(myRetSensorStats);
    for (int i = 0; i < myRetSensorStats.size(); i++) {
        myStats.add((StatElement) myRetSensorStats.get(i));
    }
    if (LogSettings.DEBUG) {
        Log.d(TAG, "Result " + myStats.toString());
    }
    return myStats;
}
Also used : Context(android.content.Context) SharedPreferences(android.content.SharedPreferences) StatElement(com.asksven.android.common.privateapiproxies.StatElement) ArrayList(java.util.ArrayList) SensorUsage(com.asksven.android.common.privateapiproxies.SensorUsage) Notification(com.asksven.android.common.privateapiproxies.Notification) SuppressLint(android.annotation.SuppressLint)

Example 2 with SensorUsage

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

the class StatsProvider method getCurrentSensorStatList.

public ArrayList<StatElement> getCurrentSensorStatList(boolean bFilter) throws Exception {
    Context ctx = BbsApplication.getAppContext();
    ArrayList<StatElement> myRetStats = new ArrayList<StatElement>();
    // stop straight away of root features are disabled
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    boolean permsNotNeeded = sharedPrefs.getBoolean("ignore_system_app", false);
    BatteryStatsProxy mStats = BatteryStatsProxy.getInstance(ctx);
    int statsType = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        statsType = BatteryStatsTypesLolipop.STATS_CURRENT;
    } else {
        statsType = BatteryStatsTypes.STATS_CURRENT;
    }
    long elapsedRealtime = SystemClock.elapsedRealtime();
    ArrayList<SensorUsage> mySensorStats = mStats.getSensorStats(ctx, elapsedRealtime, statsType);
    ArrayList<SensorUsage> myStats = new ArrayList<SensorUsage>();
    for (int i = 0; i < mySensorStats.size(); i++) {
        SensorUsage sensor = (SensorUsage) mySensorStats.get(i);
        if (sensor != null) {
            if ((!bFilter) || ((sensor.getTotal()) > 0)) {
                myStats.add(sensor);
            }
        }
    }
    Collections.sort(myStats);
    for (int i = 0; i < myStats.size(); i++) {
        myRetStats.add((StatElement) myStats.get(i));
    }
    if (LogSettings.DEBUG) {
        Log.d(TAG, "Result " + myStats.toString());
    }
    return myRetStats;
}
Also used : Context(android.content.Context) SharedPreferences(android.content.SharedPreferences) StatElement(com.asksven.android.common.privateapiproxies.StatElement) ArrayList(java.util.ArrayList) SensorUsage(com.asksven.android.common.privateapiproxies.SensorUsage) BatteryStatsProxy(com.asksven.android.common.privateapiproxies.BatteryStatsProxy) SuppressLint(android.annotation.SuppressLint)

Example 3 with SensorUsage

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

the class StatsAdapter method getView.

public View getView(int position, View convertView, ViewGroup viewGroup) {
    StatElement entry = m_listData.get(position);
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.m_context);
    boolean bShowBars = sharedPrefs.getBoolean("show_gauge", false);
    if (LogSettings.DEBUG) {
        Log.i(TAG, "Values: " + entry.getVals());
    }
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // depending on settings show new pie gauge or old bar gauge
        if (!bShowBars) {
            convertView = inflater.inflate(R.layout.stat_row, null);
        } else {
            convertView = inflater.inflate(R.layout.stat_row_gauge, null);
        }
    }
    final float scale = this.m_context.getResources().getDisplayMetrics().density;
    TextView tvName = (TextView) convertView.findViewById(R.id.TextViewName);
    // ///////////////////////////////////////
    // we do some stuff here to handle settings about font size and thumbnail size
    String fontSize = sharedPrefs.getString("medium_font_size", "16");
    int mediumFontSize = Integer.parseInt(fontSize);
    // we need to change "since" fontsize
    tvName.setTextSize(TypedValue.COMPLEX_UNIT_SP, mediumFontSize);
    // We need to handle an exception here: Sensors do not have a name so we use the fqn instead
    if (entry instanceof SensorUsage) {
        tvName.setText(entry.getFqn(UidNameResolver.getInstance()));
    } else {
        tvName.setText(entry.getName());
    }
    boolean bShowKb = sharedPrefs.getBoolean("enable_kb", true);
    ImageView iconKb = (ImageView) convertView.findViewById(R.id.imageKB);
    iconKb.setVisibility(View.INVISIBLE);
    TextView tvFqn = (TextView) convertView.findViewById(R.id.TextViewFqn);
    tvFqn.setText(entry.getFqn(UidNameResolver.getInstance()));
    TextView tvData = (TextView) convertView.findViewById(R.id.TextViewData);
    // for alarms the values is wakeups per hour so we need to take the time as reference for the text
    if (entry instanceof Alarm) {
        tvData.setText(entry.getData((long) m_timeSince));
    } else {
        tvData.setText(entry.getData((long) m_maxValue));
    }
    // LinearLayout myLayout = (LinearLayout) convertView.findViewById(R.id.LinearLayoutBar);
    LinearLayout myFqnLayout = (LinearLayout) convertView.findViewById(R.id.LinearLayoutFqn);
    LinearLayout myRow = (LinearLayout) convertView.findViewById(R.id.LinearLayoutEntry);
    // long press for "copy to clipboard"
    convertView.setOnLongClickListener(new OnItemLongClickListener(position));
    if (!bShowBars) {
        GraphablePie gauge = (GraphablePie) convertView.findViewById(R.id.Gauge);
        // ///////////////////////////////////////
        // we do some stuff here to handle settings about font size and thumbnail size
        String iconDim = sharedPrefs.getString("thumbnail_size", "56");
        int iconSize = Integer.parseInt(iconDim);
        int pixels = (int) (iconSize * scale + 0.5f);
        // we need to change "since" fontsize
        gauge.getLayoutParams().height = pixels;
        gauge.getLayoutParams().width = pixels;
        gauge.requestLayout();
        // //////////////////////////////////////////////////////////////////////////////////
        if (entry instanceof NetworkUsage) {
            gauge.setValue(entry.getValues()[0], ((NetworkUsage) entry).getTotal());
        } else {
            double max = m_maxValue;
            // avoid rounding errors leading to values > 100 %
            if (entry.getValues()[0] > max) {
                max = entry.getValues()[0];
                Log.i(TAG, "Upping gauge max to " + max);
            }
            gauge.setValue(entry.getValues()[0], max);
        }
    } else {
        GraphableBars buttonBar = (GraphableBars) convertView.findViewById(R.id.ButtonBar);
        int iHeight = 10;
        try {
            iHeight = Integer.valueOf(sharedPrefs.getString("graph_bar_height", "10"));
        } catch (Exception e) {
            iHeight = 10;
        }
        if (iHeight == 0) {
            iHeight = 10;
        }
        buttonBar.setMinimumHeight(iHeight);
        buttonBar.setName(entry.getName());
        buttonBar.setValues(entry.getValues(), m_maxValue);
    }
    ImageView iconView = (ImageView) convertView.findViewById(R.id.icon);
    LinearLayout iconLayout = (LinearLayout) convertView.findViewById(R.id.LayoutIcon);
    // ///////////////////////////////////////
    // we do some stuff here to handle settings about font size and thumbnail size
    String iconDim = sharedPrefs.getString("thumbnail_size", "56");
    int iconSize = Integer.parseInt(iconDim);
    int pixels = (int) (iconSize * scale + 0.5f);
    // we need to change "since" fontsize
    iconView.getLayoutParams().width = pixels;
    iconView.getLayoutParams().height = pixels;
    iconView.requestLayout();
    // show / hide fqn text
    if ((entry instanceof Process) || (entry instanceof State) || (entry instanceof Misc) || (entry instanceof NativeKernelWakelock) || (entry instanceof Alarm) || (entry instanceof SensorUsage)) {
        myFqnLayout.setVisibility(View.GONE);
    } else {
        myFqnLayout.setVisibility(View.VISIBLE);
    }
    // show / hide package icons (we show / hide the whole layout as it contains a margin that must be hidded as well
    if ((entry instanceof NativeKernelWakelock) || (entry instanceof State) || (entry instanceof Misc)) {
        iconView.setVisibility(View.GONE);
    } else {
        iconView.setVisibility(View.VISIBLE);
        iconView.setImageDrawable(entry.getIcon(UidNameResolver.getInstance()));
        // set a click listener for the list
        iconView.setOnClickListener(new OnPackageClickListener(position));
    }
    // add on click listener for the list entry if details are availble
    if ((entry instanceof Alarm) || (entry instanceof NativeKernelWakelock) || (entry instanceof SensorUsage)) {
        convertView.setOnClickListener(new OnItemClickListener(position));
    }
    return convertView;
}
Also used : SharedPreferences(android.content.SharedPreferences) NetworkUsage(com.asksven.android.common.privateapiproxies.NetworkUsage) Misc(com.asksven.android.common.privateapiproxies.Misc) SensorUsage(com.asksven.android.common.privateapiproxies.SensorUsage) Process(com.asksven.android.common.privateapiproxies.Process) SuppressLint(android.annotation.SuppressLint) NativeKernelWakelock(com.asksven.android.common.privateapiproxies.NativeKernelWakelock) State(com.asksven.android.common.kernelutils.State) StatElement(com.asksven.android.common.privateapiproxies.StatElement) LayoutInflater(android.view.LayoutInflater) Alarm(com.asksven.android.common.privateapiproxies.Alarm) GraphablePie(com.asksven.betterbatterystats.widgets.GraphablePie) TextView(android.widget.TextView) ImageView(android.widget.ImageView) LinearLayout(android.widget.LinearLayout) GraphableBars(com.asksven.betterbatterystats.widgets.GraphableBars)

Aggregations

SuppressLint (android.annotation.SuppressLint)3 SharedPreferences (android.content.SharedPreferences)3 SensorUsage (com.asksven.android.common.privateapiproxies.SensorUsage)3 StatElement (com.asksven.android.common.privateapiproxies.StatElement)3 Context (android.content.Context)2 ArrayList (java.util.ArrayList)2 LayoutInflater (android.view.LayoutInflater)1 ImageView (android.widget.ImageView)1 LinearLayout (android.widget.LinearLayout)1 TextView (android.widget.TextView)1 State (com.asksven.android.common.kernelutils.State)1 Alarm (com.asksven.android.common.privateapiproxies.Alarm)1 BatteryStatsProxy (com.asksven.android.common.privateapiproxies.BatteryStatsProxy)1 Misc (com.asksven.android.common.privateapiproxies.Misc)1 NativeKernelWakelock (com.asksven.android.common.privateapiproxies.NativeKernelWakelock)1 NetworkUsage (com.asksven.android.common.privateapiproxies.NetworkUsage)1 Notification (com.asksven.android.common.privateapiproxies.Notification)1 Process (com.asksven.android.common.privateapiproxies.Process)1 GraphableBars (com.asksven.betterbatterystats.widgets.GraphableBars)1 GraphablePie (com.asksven.betterbatterystats.widgets.GraphablePie)1