package com.futurice.android.reservator;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.WindowManager;
import android.widget.Toast;

import com.futurice.android.reservator.common.PreferenceManager;
import com.futurice.android.reservator.model.ReservatorException;
import com.futurice.android.reservator.model.Room;


public class ReservatorActivity extends Activity {

    private final ReservatorAppHandler handler = new ReservatorAppHandler();
    private GoToFavouriteRoom goToFavouriteRoomRunable;
    protected boolean havePermissions = false;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        goToFavouriteRoomRunable = new GoToFavouriteRoom(this);
    }

    public void onResume() {
        super.onResume();
        startAutoGoToFavouriteRoom();
    }

    public void onPause() {
        super.onPause();
        stopAutoGoToFavouriteRoom();
    }

    public void onUserInteraction() {
        super.onUserInteraction();
        stopAutoGoToFavouriteRoom();
        startAutoGoToFavouriteRoom();
    }


    /**
     * @return Identical to getApplication, but returns a ReservatorApplication.
     */
    public ReservatorApplication getResApplication() {
        return (ReservatorApplication) getApplication();
    }

    /**
     * Hook to execute actions when the activity has been prehended
     */
    public void onPrehended() {

    }

    /**
     * @return false to forbid the application to prehend the activity and go to favourite room, true to allow that.
     */
    protected Boolean isPrehensible() {
        return false;
    }

    private void startAutoGoToFavouriteRoom() {
        if (isPrehensible()) {
            handler.postDelayed(goToFavouriteRoomRunable, 60000);
        }
    }

    private void stopAutoGoToFavouriteRoom() {
        handler.removeCallbacks(goToFavouriteRoomRunable);
    }

    static class ReservatorAppHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            return;
        }
    }

    class GoToFavouriteRoom implements Runnable {

        ReservatorActivity activity;

        public GoToFavouriteRoom(ReservatorActivity anAct) {
            activity = anAct;
        }

        @Override
        public void run() {
            String roomName = PreferenceManager.getInstance(getApplicationContext()).getSelectedRoom();
            if (roomName != null) {
                Room room;
                try {
                    room = activity.getResApplication().getDataProxy().getRoomWithName(roomName);
                } catch (ReservatorException ex) {
                    Toast err = Toast.makeText(activity, ex.getMessage(),
                            Toast.LENGTH_LONG);
                    err.show();
                    return;
                }
                RoomActivity.startWith(activity, room);
            }
            activity.onPrehended();
        }
    }

}

@@ -64,16 +64,192 @@
 tivity;%0A
+import android.content.BroadcastReceiver;%0Aimport android.content.Context;%0Aimport android.content.Intent;%0Aimport android.content.IntentFilter;%0Aimport android.os.BatteryManager;%0A
 import a
@@ -320,16 +320,41 @@
 essage;%0A
+import android.util.Log;%0A
 import a
@@ -830,107 +830,742 @@
 se;%0A
-%0A    public void onCreate(Bundle savedInstanceState) %7B%0A        super.onCreate(savedInstanceState);%0A
+    BatteryStateReceiver batteryStateReceiver = new BatteryStateReceiver();%0A    IntentFilter filter = new IntentFilter();%0A%0A    public void onCreate(Bundle savedInstanceState) %7B%0A        super.onCreate(savedInstanceState);%0A        filter.addAction(Intent.ACTION_POWER_CONNECTED);%0A        filter.addAction(Intent.ACTION_POWER_DISCONNECTED);%0A        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);%0A        Intent batteryStatus = registerReceiver(null, ifilter);%0A        int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);%0A        boolean isCharging = (status == BatteryManager.BATTERY_STATUS_CHARGING %7C%7C status ==%0A            BatteryManager.BATTERY_STATUS_FULL);%0A        if (isCharging) %7B%0A    
     
@@ -1630,21 +1630,69 @@
 LAG_
-FULL
+KEEP_
 SCREEN
- %7C 
+_ON);%0A        %7D%0A        this.getWindow().addFlags(
 Wind
@@ -1715,38 +1715,34 @@
 Params.FLAG_
-KEEP_
+FULL
 SCREEN
-_ON
 );%0A        g
@@ -1888,32 +1888,88 @@
 avouriteRoom();%0A
+        registerReceiver(batteryStateReceiver, filter);%0A
     %7D%0A%0A    publi
@@ -2045,24 +2045,74 @@
 riteRoom();%0A
+        unregisterReceiver(batteryStateReceiver);%0A
     %7D%0A%0A    p
@@ -4137,10 +4137,733 @@
 %0A    %7D%0A%0A
+    class BatteryStateReceiver extends BroadcastReceiver %7B%0A%0A        private int status = -1;%0A%0A        @Override%0A        public void onReceive(Context context, Intent intent) %7B%0A            status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);%0A            if (isCharging()) %7B%0A                ReservatorActivity.this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);%0A            %7D else %7B%0A                ReservatorActivity.this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);%0A            %7D%0A        %7D%0A%0A        public boolean isCharging() %7B%0A            return (status == BatteryManager.BATTERY_STATUS_CHARGING %7C%7C status == BatteryManager.BATTERY_STATUS_FULL);%0A        %7D%0A    %7D%0A
 %7D%0A

Make the screen turn off if the battery is neither full not charging.