bring back RelayService, which RelayReceiver now delegates to for the

work of pinging relay on timer.  That work, which can cause receiver
to exceed its timeout if there are e.g. problems resolving hostnames,
must be done in a thread and Receivers aren't supposed to have
threads.  Seems to work as well as it did before and also fixes
force-restart bugs when the network is slow/unavailable.
This commit is contained in:
Andy2 2011-02-21 18:12:15 -08:00
parent 318b820ea7
commit a4e995742f
3 changed files with 99 additions and 43 deletions

View file

@ -81,6 +81,7 @@
android:theme="@android:style/Theme.Light"
android:screenOrientation="sensor"
android:configChanges="keyboardHidden|orientation"
android:launchMode="singleTask"
>
</activity>
@ -114,5 +115,7 @@
android:label="@string/chat_title"
/>
<service android:name="RelayService"/>
</application>
</manifest>

View file

@ -57,49 +57,8 @@ public class RelayReceiver extends BroadcastReceiver {
// if we're sure to finish in 10 seconds and if it'll always
// result in posting a notification. Some scenarios
query_relay( context );
// Intent service = new Intent( context, RelayService.class );
// context.startService( service );
}
private void query_relay( Context context )
{
// Utils.logf( "query_relay" );
String[] relayIDs = NetUtils.QueryRelay( context );
// At this point any changes have already been made to the
// games. Need to refresh
if ( null != relayIDs ) {
if ( !DispatchNotify.tryHandle( context, relayIDs ) ) {
setupNotification( context, relayIDs );
}
}
}
private void setupNotification( Context context, String[] relayIDs )
{
Intent intent = new Intent( context, DispatchNotify.class );
//intent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
intent.putExtra( context.getString(R.string.relayids_extra),
relayIDs );
PendingIntent pi = PendingIntent.
getActivity( context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT );
String title = context.getString(R.string.notify_title);
Notification notification =
new Notification( R.drawable.icon48x48, title,
System.currentTimeMillis() );
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.
setLatestEventInfo( context, title,
context.getString(R.string.notify_body), pi );
NotificationManager nm = (NotificationManager)
context.getSystemService( Context.NOTIFICATION_SERVICE );
nm.notify( R.string.relayids_extra, notification );
Intent service = new Intent( context, RelayService.class );
context.startService( service );
}
public static void RestartTimer( Context context )

View file

@ -0,0 +1,94 @@
/* -*- compile-command: "cd ../../../../../; ant install"; -*- */
/*
* Copyright 2010 - 2011 by Eric House (xwords@eehouse.org). All
* rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.eehouse.android.xw4;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import javax.net.SocketFactory;
import java.net.InetAddress;
import java.net.Socket;
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.util.ArrayList;
import org.eehouse.android.xw4.jni.GameSummary;
public class RelayService extends Service {
@Override
public void onCreate()
{
super.onCreate();
Thread thread = new Thread( null, new Runnable() {
public void run() {
String[] relayIDs = NetUtils.QueryRelay( RelayService.this );
if ( null != relayIDs ) {
if ( !DispatchNotify.tryHandle( RelayService.this,
relayIDs ) ) {
setupNotification( relayIDs );
}
}
RelayService.this.stopSelf();
}
}, getClass().getName() );
thread.start();
}
@Override
public IBinder onBind( Intent intent )
{
return null;
}
private void setupNotification( String[] relayIDs )
{
Intent intent = new Intent( this, DispatchNotify.class );
//intent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
intent.putExtra( getString(R.string.relayids_extra),
relayIDs );
PendingIntent pi = PendingIntent.
getActivity( this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT );
String title = getString(R.string.notify_title);
Notification notification =
new Notification( R.drawable.icon48x48, title,
System.currentTimeMillis() );
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.
setLatestEventInfo( this, title,
getString(R.string.notify_body), pi );
NotificationManager nm = (NotificationManager)
getSystemService( Context.NOTIFICATION_SERVICE );
nm.notify( R.string.relayids_extra, notification );
}
}