/*
* Copyright 2013 two forty four a.m. LLC <http://www.twofortyfouram.com>
*
* 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 tv.piratemedia.lightcontroler.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
import java.util.Locale;
import tv.piratemedia.lightcontroler.Constants;
import tv.piratemedia.lightcontroler.DataTypes.TaskerCommand;
import tv.piratemedia.lightcontroler.api.ControlProviders;
import tv.piratemedia.lightcontroler.bundle.BundleScrubber;
import tv.piratemedia.lightcontroler.bundle.PluginBundleManager;
import tv.piratemedia.lightcontroler.controlCommands;
import tv.piratemedia.lightcontroler.ui.EditActivity;
/**
* This is the "fire" BroadcastReceiver for a Locale Plug-in setting.
*
* @see com.twofortyfouram.locale.Intent#ACTION_FIRE_SETTING
* @see com.twofortyfouram.locale.Intent#EXTRA_BUNDLE
*/
public final class FireReceiver extends BroadcastReceiver
{
/**
* @param context {@inheritDoc}.
* @param intent the incoming {@link com.twofortyfouram.locale.Intent#ACTION_FIRE_SETTING} Intent. This
* should contain the {@link com.twofortyfouram.locale.Intent#EXTRA_BUNDLE} that was saved by
* {@link EditActivity} and later broadcast by Locale.
*/
@Override
public void onReceive(final Context context, final Intent intent)
{
/*
* Always be strict on input parameters! A malicious third-party app could send a malformed Intent.
*/
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
if (!com.twofortyfouram.locale.Intent.ACTION_FIRE_SETTING.equals(intent.getAction()))
{
if (Constants.IS_LOGGABLE)
{
Log.e(Constants.LOG_TAG,
String.format(Locale.US, "Received unexpected Intent action %s", intent.getAction())); //$NON-NLS-1$
}
return;
}
BundleScrubber.scrub(intent);
final Bundle bundle = intent.getBundleExtra(com.twofortyfouram.locale.Intent.EXTRA_BUNDLE);
BundleScrubber.scrub(bundle);
if (PluginBundleManager.isBundleValid(bundle))
{
System.out.println("starting");
final String message = bundle.getString(PluginBundleManager.BUNDLE_EXTRA_STRING_MESSAGE);
controlCommands Controller;
Controller = new controlCommands(context, null);
String[] _in = message.split(":");
String[][] in = new String[_in.length][3];
for (int i = 0; i < in.length; i++) {
in[i] = _in[i].split(";");
}
for (int i = in.length - 1; i >= 0; i--) {
TaskerCommand.TASKTYPE task = TaskerCommand.TASKTYPE.values()[Integer.parseInt(in[i][1])];
int zone = Integer.parseInt(in[i][0]);
String type = ControlProviders.ZONE_TYPE_COLOR;
if(zone > 4) {
//white
if(zone > 8) {
zone = 0;
} else {
zone = zone - 4;
}
type = ControlProviders.ZONE_TYPE_WHITE;
}
if (task == TaskerCommand.TASKTYPE.ON) {
Controller.LightsOn(type, zone);
}
else if (task == TaskerCommand.TASKTYPE.OFF) {
Controller.LightsOff(type, zone);
}
else if (task == TaskerCommand.TASKTYPE.WHITE) {
Controller.setToWhite(type, zone);
}
else if (task == TaskerCommand.TASKTYPE.COLOR) {
Controller.setColor(type, zone, Integer.parseInt(in[i][2]));
}
else if (task == TaskerCommand.TASKTYPE.BRIGHTNESS) {
Controller.setBrightness(type, zone, Integer.parseInt(in[i][2]));
}
}
}
}
}