/*
*  Copyright 2012 Erkan Molla
*
*  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 com.em.batterywidget.preferences.Preferences;

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.util.Log;
import android.view.View;
import android.widget.RemoteViews;


public class BatteryUpdateService extends Service {
	
    private BatteryReceiver  mBatteryReceiver = null;
    
    private int level;
    private int status;
        
    
    @Override
    public void onStart(Intent intent, int id){
 
        if (mBatteryReceiver == null)
        {
        	registerNewReceiver();
        }

        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        
        RemoteViews widgetView = getWidgetRemoteView();
        	Log.d("TAG","out");
       	if (this.getResources().getBoolean(R.bool.lowVersion))
       	{	Log.d("TAG","in1");
       		manager.updateAppWidget(new ComponentName(this, BatteryWidget.class), widgetView);
       	}
       	else
       	{Log.d("TAG","in2");
       		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(){
    	
        IntentFilter intentFilter 	= new IntentFilter();
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        intentFilter.addAction(Intent.ACTION_BATTERY_LOW);
        
        mBatteryReceiver = new BatteryReceiver();
        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(level, charge, prefSettings);
        
        /****/
        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;
    }
    
}

@@ -976,33 +976,8 @@
 er;%0A
-import android.util.Log;%0A
 impo
@@ -1512,36 +1512,16 @@
     
-%09Log.d(%22TAG%22,%22out%22);
 %0A       
 %09if 
@@ -1516,17 +1516,17 @@
 %0A       
-%09
+ 
 if (this
@@ -1583,39 +1583,20 @@
     
-%09%7B%09Log.d(%22TAG%22,%22in1%22);%0A
+ %7B%0A 
        %09
 %09man
@@ -1583,33 +1583,32 @@
      %7B%0A        %09
-%09
 manager.updateAp
@@ -1673,24 +1673,25 @@
 etView);
+%09
 %0A       
 %09%7D%0A     
@@ -1682,17 +1682,17 @@
 %0A       
-%09
+ 
 %7D%0A      
@@ -1692,17 +1692,26 @@
 %0A       
-%09
+ %0A        
 else%0A   
@@ -1718,37 +1718,18 @@
     
-%09%7BLog.d(%22TAG%22,%22in2%22);
+ %7B
 %0A       
 %09%09ma
@@ -1724,17 +1724,17 @@
 %0A       
-%09
+ 
 %09manager
@@ -1811,24 +1811,26 @@
 etView);
+%09%09
 %0A       
 %09%7D      
@@ -1825,18 +1825,19 @@
     
-%09%7D
+ %7D%0A
         
 %09%0A  
@@ -1828,25 +1828,24 @@
   %7D%0A        
-%09
 %0A    %7D%0A%0A%0A   
@@ -4512,18 +4512,16 @@
         
-//
 Notifica
@@ -4597,18 +4597,16 @@
         
-//
 mNotific

Remove log commands