mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-30 08:34:16 +01:00
download and install upgrade .apk files where possible. Download is
via the existing DictImportActivity, which should also be used for dict upgrade downloads to keep the UI consistent.
This commit is contained in:
parent
302ad04ef9
commit
9130c8da50
5 changed files with 135 additions and 27 deletions
|
@ -21,31 +21,43 @@
|
|||
package org.eehouse.android.xw4;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.os.AsyncTask;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.view.Window;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
import java.io.InputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
public class DictImportActivity extends XWActivity {
|
||||
|
||||
public static final String APK_EXTRA = "APK";
|
||||
|
||||
private class DownloadFilesTask extends AsyncTask<Uri, Integer, Long> {
|
||||
private String m_saved = null;
|
||||
private boolean m_isApp = false;
|
||||
private File m_appFile = null;
|
||||
|
||||
public DownloadFilesTask( boolean isApp )
|
||||
{
|
||||
super();
|
||||
m_isApp = isApp;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Long doInBackground( Uri... uris )
|
||||
{
|
||||
m_saved = null;
|
||||
m_appFile = null;
|
||||
|
||||
int count = uris.length;
|
||||
Assert.assertTrue( 1 == count );
|
||||
long totalSize = 0;
|
||||
for ( int ii = 0; ii < count; ii++ ) {
|
||||
Uri uri = uris[ii];
|
||||
DbgUtils.logf( "trying %s", uri );
|
||||
|
@ -55,7 +67,12 @@ public class DictImportActivity extends XWActivity {
|
|||
uri.getSchemeSpecificPart(),
|
||||
uri.getFragment() );
|
||||
InputStream is = jUri.toURL().openStream();
|
||||
m_saved = saveDict( is, uri.getPath() );
|
||||
String name = basename( uri.getPath() );
|
||||
if ( m_isApp ) {
|
||||
m_appFile = saveToDownloads( is, name );
|
||||
} else {
|
||||
m_saved = saveDict( is, name );
|
||||
}
|
||||
is.close();
|
||||
} catch ( java.net.URISyntaxException use ) {
|
||||
DbgUtils.loge( use );
|
||||
|
@ -65,7 +82,7 @@ public class DictImportActivity extends XWActivity {
|
|||
DbgUtils.loge( ioe );
|
||||
}
|
||||
}
|
||||
return totalSize;
|
||||
return new Long(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -77,6 +94,10 @@ public class DictImportActivity extends XWActivity {
|
|||
XWPrefs.getDefaultLoc( DictImportActivity.this );
|
||||
DictLangCache.inval( DictImportActivity.this, m_saved,
|
||||
loc, true );
|
||||
} else if ( null != m_appFile ) {
|
||||
// launch the installer
|
||||
Intent intent = Utils.makeInstallIntent( m_appFile );
|
||||
startActivity( intent );
|
||||
}
|
||||
finish();
|
||||
}
|
||||
|
@ -86,6 +107,7 @@ public class DictImportActivity extends XWActivity {
|
|||
protected void onCreate( Bundle savedInstanceState )
|
||||
{
|
||||
super.onCreate( savedInstanceState );
|
||||
DownloadFilesTask dft = null;
|
||||
|
||||
requestWindowFeature( Window.FEATURE_LEFT_ICON );
|
||||
setContentView( R.layout.import_dict );
|
||||
|
@ -96,27 +118,60 @@ public class DictImportActivity extends XWActivity {
|
|||
|
||||
Intent intent = getIntent();
|
||||
Uri uri = intent.getData();
|
||||
if ( null != uri) {
|
||||
if ( null != intent.getType()
|
||||
if ( null == uri ) {
|
||||
String url = intent.getStringExtra( APK_EXTRA );
|
||||
if ( null != url ) {
|
||||
dft = new DownloadFilesTask( true );
|
||||
uri = Uri.parse(url);
|
||||
}
|
||||
} else if ( null != intent.getType()
|
||||
&& intent.getType().equals( "application/x-xwordsdict" ) ) {
|
||||
DbgUtils.logf( "based on MIME type" );
|
||||
new DownloadFilesTask().execute( uri );
|
||||
dft = new DownloadFilesTask( false );
|
||||
} else if ( uri.toString().endsWith( XWConstants.DICT_EXTN ) ) {
|
||||
String txt = getString( R.string.downloading_dictf,
|
||||
basename( uri.getPath()) );
|
||||
TextView view = (TextView)findViewById( R.id.dwnld_message );
|
||||
view.setText( txt );
|
||||
new DownloadFilesTask().execute( uri );
|
||||
} else {
|
||||
DbgUtils.logf( "bogus intent: %s/%s", intent.getType(), uri );
|
||||
finish();
|
||||
dft = new DownloadFilesTask( false );
|
||||
}
|
||||
|
||||
if ( null == dft ) {
|
||||
finish();
|
||||
} else {
|
||||
dft.execute( uri );
|
||||
}
|
||||
}
|
||||
|
||||
private String saveDict( InputStream inputStream, String path )
|
||||
private File saveToDownloads( InputStream is, String name )
|
||||
{
|
||||
boolean success = false;
|
||||
File appFile = new File( DictUtils.getDownloadDir( this ), name );
|
||||
Assert.assertNotNull( appFile );
|
||||
|
||||
byte[] buf = new byte[1024*4];
|
||||
try {
|
||||
FileOutputStream fos = new FileOutputStream( appFile );
|
||||
int nRead;
|
||||
while ( 0 <= (nRead = is.read( buf, 0, buf.length )) ) {
|
||||
fos.write( buf, 0, nRead );
|
||||
}
|
||||
fos.close();
|
||||
success = true;
|
||||
} catch ( java.io.FileNotFoundException fnf ) {
|
||||
DbgUtils.loge( fnf );
|
||||
} catch ( java.io.IOException ioe ) {
|
||||
DbgUtils.loge( ioe );
|
||||
}
|
||||
|
||||
if ( !success ) {
|
||||
appFile.delete();
|
||||
appFile = null;
|
||||
}
|
||||
return appFile;
|
||||
}
|
||||
|
||||
private String saveDict( InputStream inputStream, String name )
|
||||
{
|
||||
String name = basename( path );
|
||||
DictUtils.DictLoc loc = XWPrefs.getDefaultLoc( this );
|
||||
if ( !DictUtils.saveDict( this, inputStream, name, loc ) ) {
|
||||
name = null;
|
||||
|
|
|
@ -566,7 +566,7 @@ public class DictUtils {
|
|||
return null != getDownloadDir( context );
|
||||
}
|
||||
|
||||
private static File getDownloadDir( Context context )
|
||||
public static File getDownloadDir( Context context )
|
||||
{
|
||||
File result = null;
|
||||
if ( haveWriteableSD() ) {
|
||||
|
|
|
@ -29,6 +29,7 @@ import android.content.pm.ApplicationInfo;
|
|||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.os.SystemClock;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -174,11 +175,33 @@ public class UpdateCheckReceiver extends BroadcastReceiver {
|
|||
if ( jobj.has( k_APP ) ) {
|
||||
JSONObject app = jobj.getJSONObject( k_APP );
|
||||
if ( app.has( k_URL ) ) {
|
||||
String url = app.getString( k_URL );
|
||||
ApplicationInfo ai = pm.getApplicationInfo( packageName, 0);
|
||||
String label = pm.getApplicationLabel( ai ).toString();
|
||||
Intent intent =
|
||||
new Intent( Intent.ACTION_VIEW, Uri.parse(url) );
|
||||
|
||||
// If there's a download dir AND an installer
|
||||
// app, handle this ourselves. Otherwise just
|
||||
// launch the browser
|
||||
boolean useBrowser;
|
||||
File downloads = DictUtils.getDownloadDir( context );
|
||||
if ( null == downloads ) {
|
||||
useBrowser = true;
|
||||
} else {
|
||||
File tmp = new File( downloads,
|
||||
"xx" + XWConstants.APK_EXTN );
|
||||
useBrowser = !Utils.canInstall( context, tmp );
|
||||
}
|
||||
|
||||
Intent intent;
|
||||
String url = app.getString( k_URL );
|
||||
if ( useBrowser ) {
|
||||
intent = new Intent( Intent.ACTION_VIEW,
|
||||
Uri.parse(url) );
|
||||
} else {
|
||||
intent = new Intent( context,
|
||||
DictImportActivity.class );
|
||||
intent.putExtra( DictImportActivity.APK_EXTRA, url );
|
||||
}
|
||||
|
||||
String title =
|
||||
Utils.format( context, R.string.new_app_availf, label );
|
||||
String body = context.getString( R.string.new_app_avail );
|
||||
|
|
|
@ -32,6 +32,8 @@ import android.content.DialogInterface;
|
|||
import android.content.Intent;
|
||||
import android.content.SharedPreferences.Editor;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.content.res.Configuration;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
|
@ -43,8 +45,10 @@ import android.widget.CheckBox;
|
|||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
import java.io.File;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import junit.framework.Assert;
|
||||
|
||||
|
@ -419,6 +423,29 @@ public class Utils {
|
|||
return null == s_appVersion? 0 : s_appVersion;
|
||||
}
|
||||
|
||||
public static Intent makeInstallIntent( File file )
|
||||
{
|
||||
Uri uri = Uri.parse( "file:/" + file.getPath() );
|
||||
Intent intent = new Intent( Intent.ACTION_VIEW );
|
||||
intent.setDataAndType( uri, XWConstants.APK_TYPE );
|
||||
intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
|
||||
return intent;
|
||||
}
|
||||
|
||||
// Return whether there's an app installed that can install
|
||||
public static boolean canInstall( Context context, File path )
|
||||
{
|
||||
boolean result = false;
|
||||
PackageManager pm = context.getPackageManager();
|
||||
Intent intent = makeInstallIntent( path );
|
||||
List<ResolveInfo> doers =
|
||||
pm.queryIntentActivities( intent,
|
||||
PackageManager.MATCH_DEFAULT_ONLY );
|
||||
result = 0 < doers.size();
|
||||
DbgUtils.logf( "canInstall()=>%b", result );
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void setFirstBootStatics( Context context )
|
||||
{
|
||||
int thisVersion = getAppVersion( context );
|
||||
|
|
|
@ -23,4 +23,7 @@ package org.eehouse.android.xw4;
|
|||
public interface XWConstants {
|
||||
public static final String GAME_EXTN = ".xwg";
|
||||
public static final String DICT_EXTN = ".xwd";
|
||||
public static final String APK_EXTN = ".apk";
|
||||
public static final String APK_TYPE =
|
||||
"application/vnd.android.package-archive";
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue