/*
* Copyright 2011 Greg Milette and Adam Stroud
*
* 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 root.gast.speech.tts;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.speech.tts.TextToSpeech;
import android.util.Log;
/**
* helps track the status of downloading the language data when the app requires
* it. It stores the status in a SharedPreferences field. Apps should check its
* current value by using the static methods.
*
* @author Greg Milette <<a
* href="mailto:gregorym@gmail.com">gregorym@gmail.com</a>>
*/
public class LanguageDataInstallBroadcastReceiver extends BroadcastReceiver
{
private static final String TAG = "LanguageDataInstallBroadcastReceiver";
private static final String PREFERENCES_NAME = "installedLanguageData";
private static final String WAITING_PREFERENCE_NAME =
"WAITING_PREFERENCE_NAME";
private static final Boolean WAITING_DEFAULT = false;
public LanguageDataInstallBroadcastReceiver()
{
}
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(
TextToSpeech.Engine.ACTION_TTS_DATA_INSTALLED))
{
Log.d(TAG, "language data preference: " + intent.getAction());
// clear waiting state
setWaiting(context, false);
}
}
/**
* check if the receiver is waiting for a language data install
*/
public static boolean isWaiting(Context context)
{
SharedPreferences preferences;
preferences =
context.getSharedPreferences(PREFERENCES_NAME,
Context.MODE_WORLD_READABLE);
boolean waiting =
preferences
.getBoolean(WAITING_PREFERENCE_NAME, WAITING_DEFAULT);
return waiting;
}
/**
* start waiting by setting a flag
*/
public static void setWaiting(Context context, boolean waitingStatus)
{
SharedPreferences preferences;
preferences =
context.getSharedPreferences(PREFERENCES_NAME,
Context.MODE_WORLD_WRITEABLE);
Editor editor = preferences.edit();
editor.putBoolean(WAITING_PREFERENCE_NAME, waitingStatus);
editor.commit();
}
}
// /**
// * register this receiver, calling this method is only necessary if you
// * didn't create the receiver via the manifest
// *
// * @param context
// */
// public void register(Context context)
// {
// Log.d(TAG, "registered a receiver!!");
// context.registerReceiver(this, new IntentFilter(
// TextToSpeech.Engine.ACTION_TTS_DATA_INSTALLED));
// }
//
// /**
// * unregister itself, also clear the shared preference
// */
// public void unregister(Context context)
// {
// context.unregisterReceiver(this);
// }