mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-15 15:41:24 +01:00
listen for SD card events via BroadcastReceiver registered in
AndroidManifest rather than created programatically: only with this can I get EJECT events to work. Replacement class has static register/unregister methods which DictsActivity uses.
This commit is contained in:
parent
8c0657f1cd
commit
14608d2deb
4 changed files with 107 additions and 79 deletions
|
@ -124,6 +124,18 @@
|
||||||
|
|
||||||
<service android:name="RelayService"/>
|
<service android:name="RelayService"/>
|
||||||
|
|
||||||
|
<receiver android:name=".MountEventReceiver">
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.MEDIA_MOUNTED" />
|
||||||
|
<data android:scheme="file" />
|
||||||
|
</intent-filter>
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.MEDIA_EJECT" />
|
||||||
|
<data android:scheme="file" />
|
||||||
|
</intent-filter>
|
||||||
|
</receiver>
|
||||||
|
|
||||||
|
|
||||||
<!-- <receiver android:name="NBSReceiver"> -->
|
<!-- <receiver android:name="NBSReceiver"> -->
|
||||||
<!-- <intent-filter android:priority="10"> -->
|
<!-- <intent-filter android:priority="10"> -->
|
||||||
<!-- <action android:name="android.intent.action.DATA_SMS_RECEIVED" /> -->
|
<!-- <action android:name="android.intent.action.DATA_SMS_RECEIVED" /> -->
|
||||||
|
|
|
@ -27,6 +27,7 @@ import android.app.ListActivity;
|
||||||
import android.app.ExpandableListActivity;
|
import android.app.ExpandableListActivity;
|
||||||
import android.database.DataSetObserver;
|
import android.database.DataSetObserver;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.AdapterView;
|
import android.widget.AdapterView;
|
||||||
|
@ -57,7 +58,7 @@ import org.eehouse.android.xw4.jni.CommonPrefs;
|
||||||
|
|
||||||
public class DictsActivity extends ExpandableListActivity
|
public class DictsActivity extends ExpandableListActivity
|
||||||
implements View.OnClickListener, XWListItem.DeleteCallback,
|
implements View.OnClickListener, XWListItem.DeleteCallback,
|
||||||
SDCardWatcher.SDCardNotifiee, DlgDelegate.DlgClickNotify {
|
MountEventReceiver.SDCardNotifiee, DlgDelegate.DlgClickNotify {
|
||||||
|
|
||||||
private static final String DICT_DOLAUNCH = "do_launch";
|
private static final String DICT_DOLAUNCH = "do_launch";
|
||||||
private static final String DICT_LANG_EXTRA = "use_lang";
|
private static final String DICT_LANG_EXTRA = "use_lang";
|
||||||
|
@ -88,7 +89,6 @@ public class DictsActivity extends ExpandableListActivity
|
||||||
private long m_packedPosition;
|
private long m_packedPosition;
|
||||||
private DictUtils.DictLoc m_moveFromLoc;
|
private DictUtils.DictLoc m_moveFromLoc;
|
||||||
private DictUtils.DictLoc m_moveToLoc;
|
private DictUtils.DictLoc m_moveToLoc;
|
||||||
private SDCardWatcher m_cardWatcher;
|
|
||||||
|
|
||||||
private LayoutInflater m_factory;
|
private LayoutInflater m_factory;
|
||||||
|
|
||||||
|
@ -375,7 +375,8 @@ public class DictsActivity extends ExpandableListActivity
|
||||||
protected void onResume()
|
protected void onResume()
|
||||||
{
|
{
|
||||||
super.onResume();
|
super.onResume();
|
||||||
m_cardWatcher = new SDCardWatcher( this, this );
|
MountEventReceiver.register( this );
|
||||||
|
|
||||||
mkListAdapter();
|
mkListAdapter();
|
||||||
expandGroups();
|
expandGroups();
|
||||||
}
|
}
|
||||||
|
@ -418,10 +419,9 @@ public class DictsActivity extends ExpandableListActivity
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onStop() {
|
||||||
m_cardWatcher.close();
|
MountEventReceiver.unregister( this );
|
||||||
m_cardWatcher = null;
|
super.onStop();
|
||||||
super.onPause();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onClick( View v )
|
public void onClick( View v )
|
||||||
|
@ -564,11 +564,18 @@ public class DictsActivity extends ExpandableListActivity
|
||||||
DELETE_DICT_ACTION );
|
DELETE_DICT_ACTION );
|
||||||
}
|
}
|
||||||
|
|
||||||
// SDCardWatcher.SDCardNotifiee interface
|
// MountEventReceiver.SDCardNotifiee interface
|
||||||
public void cardMounted()
|
public void cardMounted( boolean nowMounted )
|
||||||
{
|
{
|
||||||
mkListAdapter();
|
Utils.logf( "DictsActivity.cardMounted(%b)", nowMounted );
|
||||||
expandGroups();
|
// post so other SDCardNotifiee implementations get a chance
|
||||||
|
// to process first: avoid race conditions
|
||||||
|
new Handler().post( new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
mkListAdapter();
|
||||||
|
expandGroups();
|
||||||
|
}
|
||||||
|
} );
|
||||||
}
|
}
|
||||||
|
|
||||||
// DlgDelegate.DlgClickNotify interface
|
// DlgDelegate.DlgClickNotify interface
|
||||||
|
|
|
@ -0,0 +1,77 @@
|
||||||
|
/* -*- compile-command: "cd ../../../../../; ant install"; -*- */
|
||||||
|
/*
|
||||||
|
* Copyright 2009-2010 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.content.BroadcastReceiver;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
public class MountEventReceiver extends BroadcastReceiver {
|
||||||
|
|
||||||
|
public interface SDCardNotifiee {
|
||||||
|
void cardMounted( boolean nowMounted );
|
||||||
|
}
|
||||||
|
|
||||||
|
private static HashSet<SDCardNotifiee> s_procs = new HashSet<SDCardNotifiee>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onReceive( Context context, Intent intent )
|
||||||
|
{
|
||||||
|
Utils.logf( "MountEventReceiver.onReceive(%s)", intent.getAction() );
|
||||||
|
synchronized( s_procs ) {
|
||||||
|
do {
|
||||||
|
if ( s_procs.isEmpty() ) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean mounted;
|
||||||
|
String action = intent.getAction();
|
||||||
|
if ( action.equals( Intent.ACTION_MEDIA_MOUNTED ) ) {
|
||||||
|
mounted = true;
|
||||||
|
} else if ( action.equals( Intent.ACTION_MEDIA_EJECT ) ) {
|
||||||
|
mounted = false;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Iterator<SDCardNotifiee> iter = s_procs.iterator();
|
||||||
|
while ( iter.hasNext() ) {
|
||||||
|
iter.next().cardMounted( mounted );
|
||||||
|
}
|
||||||
|
} while ( false );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void register( SDCardNotifiee proc )
|
||||||
|
{
|
||||||
|
synchronized( s_procs ) {
|
||||||
|
s_procs.add( proc );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void unregister( SDCardNotifiee proc )
|
||||||
|
{
|
||||||
|
synchronized( s_procs ) {
|
||||||
|
s_procs.remove( proc );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,68 +0,0 @@
|
||||||
/* -*- compile-command: "cd ../../../../../; ant install"; -*- */
|
|
||||||
/*
|
|
||||||
* Copyright 2009-2010 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.content.BroadcastReceiver;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.content.IntentFilter;
|
|
||||||
|
|
||||||
public class SDCardWatcher {
|
|
||||||
public interface SDCardNotifiee {
|
|
||||||
void cardMounted();
|
|
||||||
}
|
|
||||||
|
|
||||||
private UmountReceiver m_rcvr;
|
|
||||||
private Context m_context;
|
|
||||||
private SDCardNotifiee m_notifiee;
|
|
||||||
|
|
||||||
private class UmountReceiver extends BroadcastReceiver {
|
|
||||||
@Override
|
|
||||||
public void onReceive( Context context, Intent intent )
|
|
||||||
{
|
|
||||||
if ( intent.getAction().
|
|
||||||
equals( Intent.ACTION_MEDIA_MOUNTED ) ) {
|
|
||||||
m_notifiee.cardMounted();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public SDCardWatcher( Context context, SDCardNotifiee notifiee )
|
|
||||||
{
|
|
||||||
m_context = context;
|
|
||||||
m_rcvr = new UmountReceiver();
|
|
||||||
m_notifiee = notifiee;
|
|
||||||
|
|
||||||
IntentFilter filter =
|
|
||||||
new IntentFilter( Intent.ACTION_MEDIA_MOUNTED );
|
|
||||||
// filter.addAction( Intent.ACTION_MEDIA_UNMOUNTED );
|
|
||||||
// filter.addAction( Intent.ACTION_MEDIA_EJECT );
|
|
||||||
filter.addDataScheme( "file" );
|
|
||||||
|
|
||||||
/*Intent intent = */context.getApplicationContext().
|
|
||||||
registerReceiver( m_rcvr, filter );
|
|
||||||
}
|
|
||||||
|
|
||||||
public void close()
|
|
||||||
{
|
|
||||||
m_context.getApplicationContext().unregisterReceiver( m_rcvr );
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue