diff options
-rwxr-xr-x | pico/AndroidManifest.xml | 7 | ||||
-rwxr-xr-x | pico/src/com/svox/pico/LangPackUninstaller.java | 45 | ||||
-rwxr-xr-x | picolanginstaller/src/com/svox/langpack/installer/InstallerActivity.java | 70 |
3 files changed, 97 insertions, 25 deletions
diff --git a/pico/AndroidManifest.xml b/pico/AndroidManifest.xml index 901b66e..367f2d2 100755 --- a/pico/AndroidManifest.xml +++ b/pico/AndroidManifest.xml @@ -15,6 +15,7 @@ <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.svox.pico" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="2" /> + <uses-permission android:name="android.permission.DELETE_PACKAGES" /> <application android:label="@string/app_name"> <activity android:name=".DownloadVoiceData" android:label="@string/app_name" @@ -32,6 +33,12 @@ </intent-filter> </receiver> + <receiver android:name=".LangPackUninstaller"> + <intent-filter> + <action android:name="android.speech.tts.engine.TTS_DATA_INSTALLED" /> + </intent-filter> + </receiver> + <activity android:name=".CheckVoiceData" android:label="@string/app_name" android:theme="@android:style/Theme.Translucent.NoTitleBar"> <intent-filter> diff --git a/pico/src/com/svox/pico/LangPackUninstaller.java b/pico/src/com/svox/pico/LangPackUninstaller.java new file mode 100755 index 0000000..ba9d73e --- /dev/null +++ b/pico/src/com/svox/pico/LangPackUninstaller.java @@ -0,0 +1,45 @@ +/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.svox.pico;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+
+/*
+ * Is notified when the language pack installer is added to the system, and runs the installer.
+ */
+public class LangPackUninstaller extends BroadcastReceiver {
+
+ private final static String TAG = "LangPackUninstaller";
+
+ private final static String INSTALLER_PACKAGE = "com.svox.langpack.installer";
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ Log.v(TAG, "about to delete " + INSTALLER_PACKAGE);
+ context.getPackageManager().deletePackage(INSTALLER_PACKAGE, null, 0);
+ }
+
+ /**
+ * Returns the name of the package that was added from the intent.
+ * @param intent the {@link Intent}
+ */
+ private static String getPackageName(Intent intent) {
+ return intent.getData().getSchemeSpecificPart();
+ }
+}
diff --git a/picolanginstaller/src/com/svox/langpack/installer/InstallerActivity.java b/picolanginstaller/src/com/svox/langpack/installer/InstallerActivity.java index 98345a4..9d5be6f 100755 --- a/picolanginstaller/src/com/svox/langpack/installer/InstallerActivity.java +++ b/picolanginstaller/src/com/svox/langpack/installer/InstallerActivity.java @@ -22,11 +22,16 @@ import android.widget.Button; public class InstallerActivity extends Activity { private static final int DATA_ROOT_DIRECTORY_REQUEST_CODE = 42; private String rootDirectory = ""; + private InstallerActivity self; + private static boolean sInstallationSuccess = false; + private static boolean sIsInstalling = false; + private final static Object sInstallerStateLock = new Object(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + self = this; Intent getRootDirectoryIntent = new Intent(); getRootDirectoryIntent.setClassName("com.svox.pico", "com.svox.pico.CheckVoiceData"); startActivityForResult(getRootDirectoryIntent, DATA_ROOT_DIRECTORY_REQUEST_CODE); @@ -37,7 +42,13 @@ public class InstallerActivity extends Activity { super.onActivityResult(requestCode, resultCode, data); if (requestCode == DATA_ROOT_DIRECTORY_REQUEST_CODE) { rootDirectory = data.getStringExtra(TextToSpeech.Engine.EXTRA_VOICE_DATA_ROOT_DIRECTORY); - runInstaller(); + // only run the installer if there isn't another one running + synchronized (sInstallerStateLock) { + if (!sIsInstalling && !sInstallationSuccess) { + sIsInstalling = true; + runInstaller(); + } + } } } @@ -56,13 +67,6 @@ public class InstallerActivity extends Activity { setContentView(R.layout.installing); } - private void uninstall() { - Intent intent = new Intent(Intent.ACTION_DELETE); - String packageName = getPackageName(); - Uri data = Uri.fromParts("package", packageName, null); - intent.setData(data); - startActivity(intent); - } private boolean unzipLangPack(InputStream stream) { FileOutputStream out; @@ -108,7 +112,7 @@ public class InstallerActivity extends Activity { } } - public class unzipper implements Runnable { + private class unzipper implements Runnable { public InputStream stream; public unzipper(InputStream is) { @@ -116,26 +120,35 @@ public class InstallerActivity extends Activity { } public void run() { - boolean succeeded = unzipLangPack(stream); - if (succeeded) { - runOnUiThread(new uninstallDisplayer()); + boolean result = unzipLangPack(stream); + synchronized (sInstallerStateLock) { + sInstallationSuccess = result; + sIsInstalling = false; + } + if (sInstallationSuccess) { + // installation completed: signal success (extra set to SUCCESS) + Intent installCompleteIntent = + new Intent(TextToSpeech.Engine.ACTION_TTS_DATA_INSTALLED); + installCompleteIntent.putExtra(TextToSpeech.Engine.EXTRA_TTS_DATA_INSTALLED, + TextToSpeech.SUCCESS); + self.sendBroadcast(installCompleteIntent); } else { - runOnUiThread(new retryDisplayer()); + // installation failed + // signal install error if the activity is finishing (can't ask the user to retry) + if (self.isFinishing()) { + Intent installCompleteIntent = + new Intent(TextToSpeech.Engine.ACTION_TTS_DATA_INSTALLED); + installCompleteIntent.putExtra(TextToSpeech.Engine.EXTRA_TTS_DATA_INSTALLED, + TextToSpeech.ERROR); + self.sendBroadcast(installCompleteIntent); + } else { + // the activity is still running, ask the user to retry. + runOnUiThread(new retryDisplayer()); + } } } } - public class uninstallDisplayer implements Runnable { - public void run() { - setContentView(R.layout.uninstall); - Button uninstallButton = (Button) findViewById(R.id.uninstallButton); - uninstallButton.setOnClickListener(new OnClickListener() { - public void onClick(View arg0) { - uninstall(); - } - }); - } - } public class retryDisplayer implements Runnable { public void run() { @@ -143,7 +156,14 @@ public class InstallerActivity extends Activity { Button retryButton = (Button) findViewById(R.id.retryButton); retryButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { - runInstaller(); + // only run the installer if there isn't another one running + // (we only get here if the installer couldn't complete successfully before) + synchronized (sInstallerStateLock) { + if (!sIsInstalling) { + sIsInstalling = true; + runInstaller(); + } + } } }); } |