package net.solutinno.util;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v4.app.Fragment;

import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class SpeechHelper
{
    private static Context mContext;
    private static final Map<String, Locale> mLanguages = new HashMap<String, Locale>();
    private static boolean mIsInitialised;

    public static void initialise(Context context) {
        mContext = context;
        mIsInitialised = false;
        initLanguages();
    }

    private static void initLanguages() {
        Intent intent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
        mContext.sendOrderedBroadcast(intent, null, mReceiver, null, Activity.RESULT_OK, null, null);
    }

    private static BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle extras = getResultExtras(true);
            assert extras != null;
            if (extras.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES)) {
                List<String> languages = extras.getStringArrayList(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);
                assert languages != null;
                for (String lang : languages) {
                    String[] loc = Iterables.toArray(Splitter.on('-').split(lang), String.class);
                    if (loc.length == 3) mLanguages.put(lang, new Locale(loc[0], loc[1], loc[2]));
                    else if (loc.length == 2) mLanguages.put(lang, new Locale(loc[0], loc[1]));
                    else if (loc.length == 1) mLanguages.put(lang, new Locale(loc[0]));
                }
            }
            mIsInitialised = true;
        }
    };

    public static Map<String, Locale> getLanguages() {
        return Maps.newHashMap(mLanguages);
    }

    public static boolean startSpeechRecognition(Fragment fragment, int requestCode, String language) {
        if (!mIsInitialised) return false;
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
        fragment.startActivityForResult(intent, requestCode);
        return true;
    }

    public static List<String> getRecognisedWords(Intent data) {
        return mIsInitialised ? data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS) : new ArrayList<String>();
    }
}

@@ -411,24 +411,49 @@
 .ArrayList;%0A
+import java.util.Arrays;%0A
 import java.
@@ -627,28 +627,100 @@
 c final 
-Map%3CString, 
+List%3CString%3E mLanguageKeys = new ArrayList%3CString%3E();%0A    private static final List%3C
 Locale%3E 
@@ -728,24 +728,29 @@
 Language
+Value
 s = new 
 HashMap%3C
@@ -745,24 +745,18 @@
 new 
-HashMap%3CString, 
+ArrayList%3C
 Loca
@@ -1525,28 +1525,24 @@
         
-List%3C
 String
-%3E
+%5B%5D
  languag
@@ -1615,16 +1615,39 @@
 NGUAGES)
+.toArray(new String%5B1%5D)
 ;%0A      
@@ -1656,23 +1656,28 @@
         
-assert 
+Arrays.sort(
 language
@@ -1677,24 +1677,17 @@
 anguages
- != null
+)
 ;%0A      
@@ -1822,24 +1822,66 @@
 ing.class);%0A
+                    Locale locale = null;%0A
             
@@ -1909,36 +1909,24 @@
 h == 3) 
-mLanguages.put(lang,
+locale =
  new Loc
@@ -1952,17 +1952,16 @@
  loc%5B2%5D)
-)
 ;%0A      
@@ -2000,36 +2000,24 @@
 h == 2) 
-mLanguages.put(lang,
+locale =
  new Loc
@@ -2035,17 +2035,16 @@
  loc%5B1%5D)
-)
 ;%0A      
@@ -2087,47 +2087,128 @@
  1) 
-mLanguages.put(lang, new Locale(loc%5B0%5D)
+locale = new Locale(loc%5B0%5D);%0A                    mLanguageKeys.add(lang);%0A                    mLanguageValues.add(locale
 );%0A 
@@ -2313,27 +2313,123 @@
 tic 
-Map%3C
 String
-, Locale%3E
+%5B%5D getLanguageKeys() %7B%0A        return mLanguageKeys.toArray(new String%5B1%5D);%0A    %7D%0A%0A    public static String%5B%5D
  get
@@ -2428,32 +2428,36 @@
 ng%5B%5D getLanguage
+Name
 s() %7B%0A        re
@@ -2458,41 +2458,234 @@
     
-return Maps.newHashMap(mLanguages
+ArrayList%3CString%3E result = new ArrayList%3CString%3E();%0A        for (Locale loc : mLanguageValues) result.add(String.format(%22%25s, %25s%22, loc.getDisplayLanguage(), loc.getDisplayCountry()));%0A        return result.toArray(new String%5B1%5D
 );%0A 

Add SettingsActivity.java; Remove default wordbook from database and put it to the SharedPreferences