Don't call pollServer from a BroadcastRecevier
[fonbot.git] / src / ro / ieval / fonbot / FonBotMainService.java
CommitLineData
8dfb76c9
MG
1package ro.ieval.fonbot;
2
3import static ro.ieval.fonbot.R.string.*;
4import static ro.ieval.fonbot.Utils.toNonNull;
5
6import java.util.Collections;
7import java.util.HashSet;
8import java.util.Set;
9
10import org.eclipse.jdt.annotation.Nullable;
11
12import ro.ieval.fonbot.Utils.OngoingEvent;
13
14import android.app.Notification;
15import android.app.NotificationManager;
16import android.app.PendingIntent;
17import android.app.Service;
18import android.content.Intent;
19import android.content.IntentFilter;
20import android.os.IBinder;
21import android.preference.PreferenceManager;
22import android.support.v4.app.NotificationCompat;
23import android.support.v4.content.LocalBroadcastManager;
24
25/*
26 * Copyright © 2013 Marius Gavrilescu
27 *
28 * This file is part of FonBot.
29 *
30 * FonBot is free software: you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation, either version 3 of the License, or
33 * (at your option) any later version.
34 *
35 * FonBot is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License
41 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
42 */
43
44/**
45 * Main service.
46 *
47 * @author Marius Gavrilescu
48 */
49public final class FonBotMainService extends Service {
50 /**
51 * Binder for the service. It lets clients get a reference to the service.
52 *
53 * @author Marius Gavrilescu <marius@ieval.ro>
54 */
55 public final class Binder extends android.os.Binder{
56 /**
57 * Get a reference to the {@link FonBotMainService}
58 * @return a reference to the {@link FonBotMainService}
59 */
60 public FonBotMainService getService(){
61 return FonBotMainService.this;
62 }
63 }
64
65 /**
66 * Broadcast action: add an ongoing event
67 */
68 public static final String ACTION_PUT_ONGOING="ro.ieval.fonbot.FonBotMainService.ACTION_PUT_ONGOING";
69 /**
70 * Broadcast action: remove an ongoing event
71 */
72 public static final String ACTION_DELETE_ONGOING="ro.ieval.fonbot.FonBotMainService.ACTION_DELETE_ONGOING";
6b9507db
MG
73 /** Broadcast action: trigger a server poll */
74 public static final String ACTION_TRIGGER_POLL="ro.ieval.fonbot.FonBotMainService.ACTION_TRIGGER_POLL";
8dfb76c9
MG
75 /**
76 * Extra: ongoing event id
77 *
78 * @see OngoingEvent
79 * @see #ACTION_DELETE_ONGOING
80 * @see #ACTION_PUT_ONGOING
81 */
82 public static final String EXTRA_ONGOING_ID="ro.ieval.fonbot.FonBotMainService.EXTRA_ONGOING_ID";
83 /**
84 * Broadcast sent when the ongoing event list is updated.
85 */
86 public static final String ACTION_ONGOING_UPDATE="ro.ieval.fonbot.FonBotMainService.ACTION_ONGOING_UPDATE";
87 /**
88 * IntentFilter for events caught by the {@link DynamicEventReceiver}
89 */
90 private static final IntentFilter DYNAMIC_BROADCAST_FILTER=new IntentFilter();
91
92 static{
93 DYNAMIC_BROADCAST_FILTER.addAction(Intent.ACTION_SCREEN_ON);
94 DYNAMIC_BROADCAST_FILTER.addAction(Intent.ACTION_BATTERY_CHANGED);
95 DYNAMIC_BROADCAST_FILTER.addAction(Intent.ACTION_HEADSET_PLUG);
96 }
97
98 /**
99 * The one instance of {@link DynamicEventReceiver}
100 */
101 private final DynamicEventReceiver receiver=new DynamicEventReceiver();
102 /**
103 * Set of ongoing events.
104 */
105 private final Set<OngoingEvent> ongoing=new HashSet<OngoingEvent>(10);
106
107 /**
108 * Get the set of ongoing events.
109 *
110 * @return a set of ongoing events
111 */
112 public Set<OngoingEvent> getOngoingEvents(){
113 return toNonNull(Collections.unmodifiableSet(ongoing));
114 }
115
116 @Override
117 public @Nullable IBinder onBind(final @Nullable Intent intent) {
118 return new Binder();
119 }
120
121 @Override
122 public void onCreate() {
123 super.onCreate();
124 registerReceiver(receiver, DYNAMIC_BROADCAST_FILTER);
125 }
126
127 @Override
128 public void onDestroy() {
129 super.onDestroy();
130 unregisterReceiver(receiver);
131 }
132
133 @Override
134 public int onStartCommand(final @Nullable Intent intent, final int flags, final int startId) {
135 if(intent!=null && intent.getAction()==ACTION_PUT_ONGOING && intent.hasExtra(EXTRA_ONGOING_ID)){
136 ongoing.add(OngoingEvent.values()[intent.getIntExtra(EXTRA_ONGOING_ID, 0)]);
137 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(ACTION_ONGOING_UPDATE));
138 }
139 if(intent!=null && intent.getAction()==ACTION_DELETE_ONGOING && intent.hasExtra(EXTRA_ONGOING_ID)){
140 ongoing.remove(OngoingEvent.values()[intent.getIntExtra(EXTRA_ONGOING_ID, 0)]);
141 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(ACTION_ONGOING_UPDATE));
142 }
143
6b9507db
MG
144 if(intent!=null && intent.getAction()==ACTION_TRIGGER_POLL)
145 Utils.pollServer(this);
146
8dfb76c9
MG
147 final boolean runForeground=PreferenceManager.getDefaultSharedPreferences(this).getBoolean("foreground", false);
148 if(!runForeground)
149 stopForeground(true);
150
151 if(runForeground||!ongoing.isEmpty()){
152 final Intent mainIntent=new Intent(this, FonBotMainActivity.class);
153 mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
154 final NotificationCompat.Builder builder=new NotificationCompat.Builder(this).
155 setContentText(getString(foreground_notification_text)).
156 setContentTitle(getString(foreground_notification_title)).
157 setSmallIcon(android.R.drawable.stat_notify_sync_noanim).
158 setPriority(NotificationCompat.PRIORITY_MIN).
159 setContentIntent(PendingIntent.getActivity(this, 0, mainIntent, 0)).
160 setOngoing(true);
161
162 final Notification notification;
163 if(ongoing.isEmpty())
164 notification=builder.build();
165 else {
166 final NotificationCompat.InboxStyle inboxBuilder=new NotificationCompat.InboxStyle(builder);
167
168 for(final OngoingEvent event : ongoing)
169 inboxBuilder.addLine(getString(event.resource));
170
171 notification=inboxBuilder.build();
172 }
173
174 if(runForeground)
175 startForeground(1337, builder.build());
176 else{
177 final NotificationManager man=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
178 man.notify(1337, notification);
179 }
180 }
181
182 return START_STICKY;
183 }
184}
This page took 0.021591 seconds and 4 git commands to generate.