1 package ro
.ieval
.fonbot
;
3 import static ro
.ieval
.fonbot
.R
.string
.*;
5 import static ro
.ieval
.fonbot
.Utils
.toNonNull
;
7 import org
.eclipse
.jdt
.annotation
.Nullable
;
9 import ro
.ieval
.fonbot
.Utils
.MessageType
;
10 import android
.content
.BroadcastReceiver
;
11 import android
.content
.Context
;
12 import android
.content
.Intent
;
13 import android
.preference
.PreferenceManager
;
16 * Copyright © 2013 Marius Gavrilescu
18 * This file is part of FonBot.
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.
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.
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/>.
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}
37 * @author Marius Gavrilescu <marius@ieval.ro>
39 public final class DynamicEventReceiver
extends BroadcastReceiver
{
42 public void onReceive(final @Nullable Context context
, final @Nullable Intent intent
) {
43 if(context
==null || intent
==null || isInitialStickyBroadcast())
46 final String action
=intent
.getAction();
48 if(action
.equals(Intent
.ACTION_SCREEN_ON
)){
49 if(PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean("poll_on_screen_on", false))
50 Utils
.pollServer(context
);
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);
58 final StringBuilder builder
=new StringBuilder(100);
60 builder
.append(toNonNull(context
.getString(headset_with_microphone
)));
62 builder
.append(toNonNull(context
.getString(headset_without_microphone
)));
67 builder
.append(toNonNull(context
.getString(plugged_in
)));
69 builder
.append(toNonNull(context
.getString(unplugged
)));
72 Utils
.sendMessage(context
,
73 toNonNull(MessageType
.HEADSET
),
74 toNonNull(builder
.toString()));