]> iEval git - fonbot.git/blob - src/ro/ieval/fonbot/FonBotMainService.java
Add retries to message sending
[fonbot.git] / src / ro / ieval / fonbot / FonBotMainService.java
1 package ro.ieval.fonbot;
2
3 import static ro.ieval.fonbot.R.string.*;
4 import static ro.ieval.fonbot.Utils.toNonNull;
5
6 import java.util.Collections;
7 import java.util.HashSet;
8 import java.util.Set;
9
10 import org.eclipse.jdt.annotation.Nullable;
11
12 import ro.ieval.fonbot.Utils.OngoingEvent;
13
14 import android.app.Notification;
15 import android.app.NotificationManager;
16 import android.app.PendingIntent;
17 import android.app.Service;
18 import android.content.Intent;
19 import android.content.IntentFilter;
20 import android.os.IBinder;
21 import android.preference.PreferenceManager;
22 import android.support.v4.app.NotificationCompat;
23 import 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 */
49 public 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";
73 /**
74 * Extra: ongoing event id
75 *
76 * @see OngoingEvent
77 * @see #ACTION_DELETE_ONGOING
78 * @see #ACTION_PUT_ONGOING
79 */
80 public static final String EXTRA_ONGOING_ID="ro.ieval.fonbot.FonBotMainService.EXTRA_ONGOING_ID";
81 /**
82 * Broadcast sent when the ongoing event list is updated.
83 */
84 public static final String ACTION_ONGOING_UPDATE="ro.ieval.fonbot.FonBotMainService.ACTION_ONGOING_UPDATE";
85 /**
86 * IntentFilter for events caught by the {@link DynamicEventReceiver}
87 */
88 private static final IntentFilter DYNAMIC_BROADCAST_FILTER=new IntentFilter();
89
90 static{
91 DYNAMIC_BROADCAST_FILTER.addAction(Intent.ACTION_SCREEN_ON);
92 DYNAMIC_BROADCAST_FILTER.addAction(Intent.ACTION_BATTERY_CHANGED);
93 DYNAMIC_BROADCAST_FILTER.addAction(Intent.ACTION_HEADSET_PLUG);
94 }
95
96 /**
97 * The one instance of {@link DynamicEventReceiver}
98 */
99 private final DynamicEventReceiver receiver=new DynamicEventReceiver();
100 /**
101 * Set of ongoing events.
102 */
103 private final Set<OngoingEvent> ongoing=new HashSet<OngoingEvent>(10);
104
105 /**
106 * Get the set of ongoing events.
107 *
108 * @return a set of ongoing events
109 */
110 public Set<OngoingEvent> getOngoingEvents(){
111 return toNonNull(Collections.unmodifiableSet(ongoing));
112 }
113
114 @Override
115 public @Nullable IBinder onBind(final @Nullable Intent intent) {
116 return new Binder();
117 }
118
119 @Override
120 public void onCreate() {
121 super.onCreate();
122 registerReceiver(receiver, DYNAMIC_BROADCAST_FILTER);
123 }
124
125 @Override
126 public void onDestroy() {
127 super.onDestroy();
128 unregisterReceiver(receiver);
129 }
130
131 @Override
132 public int onStartCommand(final @Nullable Intent intent, final int flags, final int startId) {
133 if(intent!=null && intent.getAction()==ACTION_PUT_ONGOING && intent.hasExtra(EXTRA_ONGOING_ID)){
134 ongoing.add(OngoingEvent.values()[intent.getIntExtra(EXTRA_ONGOING_ID, 0)]);
135 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(ACTION_ONGOING_UPDATE));
136 }
137 if(intent!=null && intent.getAction()==ACTION_DELETE_ONGOING && intent.hasExtra(EXTRA_ONGOING_ID)){
138 ongoing.remove(OngoingEvent.values()[intent.getIntExtra(EXTRA_ONGOING_ID, 0)]);
139 LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(ACTION_ONGOING_UPDATE));
140 }
141
142 final boolean runForeground=PreferenceManager.getDefaultSharedPreferences(this).getBoolean("foreground", false);
143 if(!runForeground)
144 stopForeground(true);
145
146 if(runForeground||!ongoing.isEmpty()){
147 final Intent mainIntent=new Intent(this, FonBotMainActivity.class);
148 mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
149 final NotificationCompat.Builder builder=new NotificationCompat.Builder(this).
150 setContentText(getString(foreground_notification_text)).
151 setContentTitle(getString(foreground_notification_title)).
152 setSmallIcon(android.R.drawable.stat_notify_sync_noanim).
153 setPriority(NotificationCompat.PRIORITY_MIN).
154 setContentIntent(PendingIntent.getActivity(this, 0, mainIntent, 0)).
155 setOngoing(true);
156
157 final Notification notification;
158 if(ongoing.isEmpty())
159 notification=builder.build();
160 else {
161 final NotificationCompat.InboxStyle inboxBuilder=new NotificationCompat.InboxStyle(builder);
162
163 for(final OngoingEvent event : ongoing)
164 inboxBuilder.addLine(getString(event.resource));
165
166 notification=inboxBuilder.build();
167 }
168
169 if(runForeground)
170 startForeground(1337, builder.build());
171 else{
172 final NotificationManager man=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
173 man.notify(1337, notification);
174 }
175 }
176
177 return START_STICKY;
178 }
179 }
This page took 0.060725 seconds and 4 git commands to generate.