PageRenderTime 17ms CodeModel.GetById 6ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/ocr/ocrservice/src/com/googlecode/eyesfree/ocr/intent/PrefsActivity.java

http://eyes-free.googlecode.com/
Java | 198 lines | 129 code | 44 blank | 25 comment | 11 complexity | 869c0eeda8b3dfbd83c6cc91b0eaa5d6 MD5 | raw file
  1/*
  2 * Copyright (C) 2011 Google Inc.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16
 17package com.googlecode.eyesfree.ocr.intent;
 18
 19import android.content.BroadcastReceiver;
 20import android.content.Context;
 21import android.content.Intent;
 22import android.content.IntentFilter;
 23import android.os.Bundle;
 24import android.os.Environment;
 25import android.preference.ListPreference;
 26import android.preference.Preference;
 27import android.preference.Preference.OnPreferenceClickListener;
 28import android.preference.PreferenceActivity;
 29import android.util.Log;
 30
 31import com.googlecode.eyesfree.ocr.R;
 32import com.googlecode.eyesfree.ocr.client.Intents;
 33import com.googlecode.eyesfree.ocr.client.Language;
 34import com.googlecode.eyesfree.ocr.client.Ocr;
 35
 36import java.util.List;
 37
 38/**
 39 * Provides access to global OCR preferences.
 40 *
 41 * @author alanv@google.com (Alan Viverette)
 42 */
 43public class PrefsActivity extends PreferenceActivity {
 44    private static final String TAG = "PrefsActivity";
 45
 46    private Preference mManagePref;
 47    private ListPreference mLangPref;
 48    private IntentFilter mFilter;
 49
 50    private String mStrManage;
 51    private String mStrLang;
 52
 53    private Ocr mOcr;
 54
 55    private boolean mInitialized;
 56
 57    @Override
 58    protected void onCreate(Bundle savedInstanceState) {
 59        super.onCreate(savedInstanceState);
 60
 61        addPreferencesFromResource(R.xml.prefs);
 62
 63        mStrManage = getString(R.string.manage_pref);
 64        mStrLang = getString(R.string.lang_pref);
 65
 66        // "Test Activities" is disabled for now...
 67        // mStrTest = getString(R.string.test_pref);
 68        // findPreference(mStrTest).setOnPreferenceClickListener(onPreferenceClick);
 69
 70        mManagePref = findPreference(mStrManage);
 71        mManagePref.setOnPreferenceClickListener(onPreferenceClick);
 72        mManagePref.setEnabled(false);
 73
 74        mLangPref = (ListPreference) findPreference(mStrLang);
 75        mLangPref.setEnabled(false);
 76
 77        mFilter = new IntentFilter();
 78        mFilter.addAction(Intents.Actions.LANGUAGES_UPDATED);
 79
 80        Ocr.InitCallback initCallback = new Ocr.InitCallback() {
 81            @Override
 82            public void onInitialized(int status) {
 83                updateLanguages();
 84
 85                mInitialized = true;
 86            }
 87        };
 88
 89        mInitialized = false;
 90        mOcr = new Ocr(this, initCallback);
 91    }
 92
 93    @Override
 94    protected void onResume() {
 95        super.onResume();
 96
 97        registerReceiver(broadcastReceiver, mFilter);
 98
 99        if (mInitialized) {
100            updateLanguages();
101        }
102    }
103
104    @Override
105    protected void onPause() {
106        unregisterReceiver(broadcastReceiver);
107
108        super.onPause();
109    }
110
111    @Override
112    protected void onDestroy() {
113        mOcr.release();
114
115        super.onDestroy();
116    }
117
118    @Override
119    public void onWindowFocusChanged(boolean hasFocus) {
120        super.onWindowFocusChanged(hasFocus);
121
122        if (hasFocus) {
123            updateLangPrefSummary();
124        }
125    }
126
127    private void updateLangPrefSummary() {
128        final String defaultLanguage = mLangPref.getValue();
129
130        if (defaultLanguage != null) {
131            mLangPref.setSummary(getString(R.string.lang_pref_summary, defaultLanguage));
132        }
133    }
134
135    private void updateLanguages() {
136        // Abort if the SD card isn't inserted
137        if (!Environment.getExternalStorageDirectory().canRead()) {
138            Log.e(TAG, "Missing SD card");
139
140            mLangPref.setEnabled(false);
141            mManagePref.setEnabled(false);
142            return;
143        }
144
145        List<Language> available = mOcr.getAvailableLanguages();
146
147        // Enable language management and abort if we're missing languages
148        if (available.isEmpty()) {
149            Log.e(TAG, "No languages installed");
150
151            mLangPref.setEnabled(false);
152            mManagePref.setEnabled(true);
153            return;
154        }
155
156        String[] entries = new String[available.size()];
157        String[] values = new String[available.size()];
158
159        for (int i = 0; i < available.size(); i++) {
160            Language lang = available.get(i);
161
162            values[i] = lang.iso_639_2;
163            entries[i] = lang.english;
164        }
165
166        mLangPref.setEntries(entries);
167        mLangPref.setEntryValues(values);
168        mLangPref.setEnabled(true);
169        mManagePref.setEnabled(true);
170    }
171
172    private final OnPreferenceClickListener onPreferenceClick = new OnPreferenceClickListener() {
173        @Override
174        public boolean onPreferenceClick(Preference preference) {
175            String key = preference.getKey();
176            Intent intent;
177
178            if (mStrManage.equals(key)) {
179                intent = new Intent(Intents.Languages.ACTION);
180            } else {
181                return false;
182            }
183
184            startActivity(intent);
185
186            return true;
187        }
188    };
189
190    private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
191        @Override
192        public void onReceive(Context context, Intent intent) {
193            if (mInitialized && intent.getAction().equals(Intents.Actions.LANGUAGES_UPDATED)) {
194                updateLanguages();
195            }
196        }
197    };
198}