forked from organicmaps/organicmaps
[andr] Base framework for background tasks.
This commit is contained in:
parent
5d2c76d856
commit
f460dbe47f
4 changed files with 202 additions and 0 deletions
|
@ -200,6 +200,20 @@
|
|||
android:launchMode="singleTop"
|
||||
android:screenOrientation="behind" >
|
||||
</activity>
|
||||
|
||||
<receiver
|
||||
android:name="com.mapswithme.maps.background.ConnectivityChangedReceiver"
|
||||
android:enabled="true"
|
||||
android:exported="true" >
|
||||
|
||||
<intent-filter>
|
||||
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
<service android:name="com.mapswithme.maps.background.WorkerService"
|
||||
android:exported="false" >
|
||||
</service>
|
||||
</application>
|
||||
|
||||
</manifest>
|
|
@ -0,0 +1,40 @@
|
|||
package com.mapswithme.maps.background;
|
||||
|
||||
import com.mapswithme.util.log.Logger;
|
||||
import com.mapswithme.util.log.SimpleLogger;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
|
||||
public class ConnectivityChangedReceiver extends BroadcastReceiver
|
||||
{
|
||||
private final static Logger l = SimpleLogger.get("MWMConnect");
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent)
|
||||
{
|
||||
if (!ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction()))
|
||||
throw new IllegalStateException("This class must listen only to CONNECTIVITY_ACTION");
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
final NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
|
||||
if (networkInfo != null)
|
||||
{
|
||||
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI)
|
||||
onWiFiConnectionChanged(networkInfo.isConnected(), context);
|
||||
}
|
||||
}
|
||||
|
||||
public void onWiFiConnectionChanged(boolean isConnected, Context context)
|
||||
{
|
||||
l.d("WiFi Connected", isConnected);
|
||||
if (isConnected)
|
||||
{
|
||||
WorkerService.startActionCheckUpdate(context);
|
||||
WorkerService.startActionPushStat(context);
|
||||
}
|
||||
}
|
||||
}
|
67
android/src/com/mapswithme/maps/background/Notifier.java
Normal file
67
android/src/com/mapswithme/maps/background/Notifier.java
Normal file
|
@ -0,0 +1,67 @@
|
|||
package com.mapswithme.maps.background;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.mapswithme.maps.R;
|
||||
import com.mapswithme.util.log.Logger;
|
||||
import com.mapswithme.util.log.SimpleLogger;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationManager;
|
||||
import android.content.Context;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import android.text.TextUtils;
|
||||
|
||||
public class Notifier
|
||||
{
|
||||
private Logger l = SimpleLogger.get("MWMNotification");
|
||||
|
||||
private final static int ID_DEF = 0x0;
|
||||
private final static int ID_UPDATE_AVAIL = 0x1;
|
||||
|
||||
private NotificationManager mNotificationManager;
|
||||
private Context mContext;
|
||||
|
||||
public Notifier(Context context)
|
||||
{
|
||||
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
public void placeNotification(String text)
|
||||
{
|
||||
l.d("Noti", text);
|
||||
|
||||
final Notification notification = getBuilder()
|
||||
.setContentText(text)
|
||||
.setTicker(text)
|
||||
.setContentTitle(text)
|
||||
.build();
|
||||
|
||||
mNotificationManager.notify(ID_DEF, notification);
|
||||
}
|
||||
|
||||
public void placeUpdateAvailable(List<CharSequence> forItems)
|
||||
{
|
||||
// TODO add real resources
|
||||
final String forItemsStr = TextUtils.join(",", forItems);
|
||||
final String title = "Map Update Available ";
|
||||
final String text = "New data available for " + forItemsStr;
|
||||
final Notification notification = getBuilder()
|
||||
.setContentTitle(title)
|
||||
.setSubText(text)
|
||||
.setTicker(title + text)
|
||||
.build();
|
||||
|
||||
mNotificationManager.notify(ID_UPDATE_AVAIL, notification);
|
||||
}
|
||||
|
||||
public NotificationCompat.Builder getBuilder()
|
||||
{
|
||||
// TODO add default initialization
|
||||
return new NotificationCompat.Builder(mContext)
|
||||
.setAutoCancel(true)
|
||||
.setSmallIcon(R.drawable.ic_launcher);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
package com.mapswithme.maps.background;
|
||||
|
||||
import com.mapswithme.util.log.Logger;
|
||||
import com.mapswithme.util.log.SimpleLogger;
|
||||
|
||||
import android.app.IntentService;
|
||||
import android.content.Intent;
|
||||
import android.content.Context;
|
||||
|
||||
/**
|
||||
* An {@link IntentService} subclass for handling asynchronous task requests in
|
||||
* a service on a separate handler thread.
|
||||
* <p>
|
||||
*/
|
||||
public class WorkerService extends IntentService
|
||||
{
|
||||
private static final String ACTION_PUSH_STATISTICS = "com.mapswithme.maps.action.stat";
|
||||
private static final String ACTION_CHECK_UPDATE = "com.mapswithme.maps.action.update";
|
||||
|
||||
private Logger l = SimpleLogger.get("MWMWorkerService");
|
||||
private Notifier mNotifier;
|
||||
|
||||
|
||||
/**
|
||||
* Starts this service to perform action with the given parameters. If the
|
||||
* service is already performing a task this action will be queued.
|
||||
*
|
||||
* @see IntentService
|
||||
*/
|
||||
public static void startActionPushStat(Context context)
|
||||
{
|
||||
Intent intent = new Intent(context, WorkerService.class);
|
||||
intent.setAction(ACTION_PUSH_STATISTICS);
|
||||
context.startService(intent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts this service to perform check update action with the given parameters. If the
|
||||
* service is already performing a task this action will be queued.
|
||||
*
|
||||
* @see IntentService
|
||||
*/
|
||||
public static void startActionCheckUpdate(Context context)
|
||||
{
|
||||
Intent intent = new Intent(context, WorkerService.class);
|
||||
intent.setAction(ACTION_CHECK_UPDATE);
|
||||
context.startService(intent);
|
||||
}
|
||||
|
||||
public WorkerService()
|
||||
{
|
||||
super("WorkerService");
|
||||
mNotifier = new Notifier(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onHandleIntent(Intent intent)
|
||||
{
|
||||
if (intent != null)
|
||||
{
|
||||
final String action = intent.getAction();
|
||||
|
||||
if (ACTION_CHECK_UPDATE.equals(action))
|
||||
handleActionCheckUpdate();
|
||||
else if (ACTION_PUSH_STATISTICS.equals(action))
|
||||
handleActionPushStat();
|
||||
}
|
||||
}
|
||||
|
||||
private void handleActionCheckUpdate()
|
||||
{
|
||||
// TODO: Handle check for update
|
||||
throw new UnsupportedOperationException("Not yet implemented");
|
||||
}
|
||||
|
||||
private void handleActionPushStat()
|
||||
{
|
||||
// TODO: Handle stat push
|
||||
throw new UnsupportedOperationException("Not yet implemented");
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue