Make custom server work when polling
[fonbot.git] / src / ro / ieval / fonbot / DynamicEventReceiver.java
CommitLineData
8dfb76c9
MG
1package ro.ieval.fonbot;
2
3import static ro.ieval.fonbot.R.string.*;
4
5import static ro.ieval.fonbot.Utils.toNonNull;
6
7import org.eclipse.jdt.annotation.Nullable;
8
9import ro.ieval.fonbot.Utils.MessageType;
10import android.content.BroadcastReceiver;
11import android.content.Context;
12import android.content.Intent;
13import android.preference.PreferenceManager;
14
15/*
16 * Copyright © 2013 Marius Gavrilescu
17 *
18 * This file is part of FonBot.
19 *
20 * FonBot is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
24 *
25 * FonBot is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
32 */
33
34/**
35 * A BroadcastReceiver that receives "dynamic" events like {@link Intent#ACTION_SCREEN_ON ACTION_SCREEN_ON}, {@link Intent#ACTION_BATTERY_CHANGED ACTION_BATTERY_CHANGED} and {@link Intent#ACTION_HEADSET_PLUG}
36 *
37 * @author Marius Gavrilescu <marius@ieval.ro>
38 */
39public final class DynamicEventReceiver extends BroadcastReceiver {
40
41 @Override
42 public void onReceive(final @Nullable Context context, final @Nullable Intent intent) {
43 if(context==null || intent==null || isInitialStickyBroadcast())
44 return;
45
46 final String action=intent.getAction();
47
48 if(action.equals(Intent.ACTION_SCREEN_ON)){
49 if(PreferenceManager.getDefaultSharedPreferences(context).getBoolean("poll_on_screen_on", false))
a97d31fb 50 new PollServerAsyncTask(context).execute();
8dfb76c9
MG
51 } else if(action.equals(Intent.ACTION_BATTERY_CHANGED))
52 Heavy.describeBatteryLevel(context, null, toNonNull(MessageType.BATTERY_CHANGED));
53 else if(action.equals(Intent.ACTION_HEADSET_PLUG)){
54 final int state=intent.getIntExtra("state", 0);
55 final String name=intent.getStringExtra("name");
56 final int microphone=intent.getIntExtra("microphone", 0);
57
58 final StringBuilder builder=new StringBuilder(100);
59 if(microphone==1)
60 builder.append(toNonNull(context.getString(headset_with_microphone)));
61 else
62 builder.append(toNonNull(context.getString(headset_without_microphone)));
63 builder.append(' ');
64 builder.append(name);
65 builder.append(' ');
66 if(state==1)
67 builder.append(toNonNull(context.getString(plugged_in)));
68 else
69 builder.append(toNonNull(context.getString(unplugged)));
70 builder.append(". ");
71
72 Utils.sendMessage(context,
73 toNonNull(MessageType.HEADSET),
74 toNonNull(builder.toString()));
75 }
76 }
77
78}
This page took 0.01411 seconds and 4 git commands to generate.