Android Open Source - Android-Battery-Widget Battery Update Service






From Project

Back to project page Android-Battery-Widget.

License

The source code is released under:

Apache License

If you think the Android project Android-Battery-Widget listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 *  Copyright 2012 Erkan Molla//from   w w  w . ja  va  2 s .c  o m
 *
 *  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.em.batterywidget;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.BatteryManager;
import android.os.IBinder;
import android.view.View;
import android.widget.RemoteViews;
import com.em.batterywidget.preferences.Preferences;

public class BatteryUpdateService extends Service {

    private BatteryReceiver mBatteryReceiver = null;
    private int             level            = 0;
    private int             status           = 0;

    @Override
    public void onStart(Intent intent, int id) {
        if (mBatteryReceiver == null) {
            registerNewReceiver();
        }

        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        RemoteViews widgetView = getWidgetRemoteView();

        if (this.getResources().getBoolean(R.bool.lowVersion)) {
            manager.updateAppWidget(new ComponentName(this, BatteryWidget.class), widgetView);
        }
        else {
            manager.updateAppWidget(new ComponentName(this, BatteryWidget_HC.class), widgetView);
        }
    }

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

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBatteryReceiver);
    }

    private void registerNewReceiver() {
        mBatteryReceiver = new BatteryReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        intentFilter.addAction(Intent.ACTION_BATTERY_LOW);
        registerReceiver(mBatteryReceiver, intentFilter);
    }

    private RemoteViews getWidgetRemoteView() {
        Preferences prefSettings = new Preferences(Constants.BATTERY_SETTINGS, this);
        Preferences prefBatteryInfo = new Preferences(Constants.BATTERY_INFO, this);

        level = prefBatteryInfo.getValue(Constants.LEVEL, 0);
        status = prefBatteryInfo.getValue(Constants.STATUS, 0);

        String color = prefSettings.getValue(Constants.TEXT_COLOUR_SETTINGS, Constants.DEFAULT_COLOUR);
        boolean charge = status == BatteryManager.BATTERY_STATUS_CHARGING;

        RemoteViews widgetView = new RemoteViews(this.getPackageName(), R.layout.widget_view);
        widgetView.setImageViewResource(R.id.battery_view, R.drawable.battery);

        widgetView.setViewVisibility(R.id.percent100, round(100) ? View.VISIBLE : View.INVISIBLE);
        widgetView.setViewVisibility(R.id.percent90, round(90) ? View.VISIBLE : View.INVISIBLE);
        widgetView.setViewVisibility(R.id.percent80, round(80) ? View.VISIBLE : View.INVISIBLE);
        widgetView.setViewVisibility(R.id.percent70, round(70) ? View.VISIBLE : View.INVISIBLE);
        widgetView.setViewVisibility(R.id.percent60, round(60) ? View.VISIBLE : View.INVISIBLE);
        widgetView.setViewVisibility(R.id.percent50, round(50) ? View.VISIBLE : View.INVISIBLE);
        widgetView.setViewVisibility(R.id.percent40, round(40) ? View.VISIBLE : View.INVISIBLE);
        widgetView.setViewVisibility(R.id.percent30, round(30) ? View.VISIBLE : View.INVISIBLE);
        widgetView.setViewVisibility(R.id.percent20, round(20) ? View.VISIBLE : View.INVISIBLE);
        widgetView.setViewVisibility(R.id.percent10, round(10) ? View.VISIBLE : View.INVISIBLE);

        widgetView.setViewVisibility(R.id.batterytext, View.VISIBLE);
        widgetView.setTextColor(R.id.batterytext, Color.parseColor(color));
        widgetView.setTextViewText(R.id.batterytext, String.valueOf(level) + "%");
        widgetView.setViewVisibility(R.id.charge_view, charge ? View.VISIBLE : View.INVISIBLE);

        NotificationsManager mNotificationsManager = new NotificationsManager(this);
        mNotificationsManager.updateNotificationIcon(prefSettings, prefBatteryInfo);

        Intent intent = new Intent(this, BatteryWidgetActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        widgetView.setOnClickPendingIntent(R.id.widget_view, pendingIntent);

        return widgetView;
    }

    private boolean round(int percent) {
        int minus = percent - 10;
        if (level <= percent && level > minus)
            return true;
        else
            return false;
    }

}




Java Source Code List

com.em.batterywidget.AboutInfoActivity.java
com.em.batterywidget.BatteryReceiver.java
com.em.batterywidget.BatteryUpdateService.java
com.em.batterywidget.BatteryWidgetActivity.java
com.em.batterywidget.BatteryWidget_HC.java
com.em.batterywidget.BatteryWidget.java
com.em.batterywidget.Constants.java
com.em.batterywidget.HistoryViewManager.java
com.em.batterywidget.NotificationsManager.java
com.em.batterywidget.OptionsManager.java
com.em.batterywidget.preferences.Preferences.java
com.em.batterywidget.storage.SQLiteDataBase.java