eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ImportVoicePreference.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (C) 2013 Reece H. Dunn
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of 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,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.reecedunn.espeak.preference;
  17. import android.app.Activity;
  18. import android.app.DownloadManager;
  19. import android.content.Context;
  20. import android.content.DialogInterface;
  21. import android.content.Intent;
  22. import android.os.AsyncTask;
  23. import android.os.Environment;
  24. import android.preference.DialogPreference;
  25. import android.util.AttributeSet;
  26. import android.view.View;
  27. import android.widget.Spinner;
  28. import com.reecedunn.espeak.CheckVoiceData;
  29. import com.reecedunn.espeak.DownloadVoiceData;
  30. import com.reecedunn.espeak.FileListAdapter;
  31. import com.reecedunn.espeak.FileUtils;
  32. import com.reecedunn.espeak.R;
  33. import java.io.File;
  34. import java.io.FileFilter;
  35. import java.io.FileInputStream;
  36. import java.io.FileNotFoundException;
  37. import java.io.IOException;
  38. import java.util.Arrays;
  39. public class ImportVoicePreference extends DialogPreference {
  40. private File mRoot;
  41. private Spinner mDictionaries;
  42. public ImportVoicePreference(Context context, AttributeSet attrs, int defStyle) {
  43. super(context, attrs, defStyle);
  44. setDialogLayoutResource(R.layout.import_voice_preference);
  45. setLayoutResource(R.layout.information_view);
  46. setPositiveButtonText(android.R.string.ok);
  47. setNegativeButtonText(android.R.string.cancel);
  48. mRoot = Environment.getExternalStorageDirectory();
  49. }
  50. public ImportVoicePreference(Context context, AttributeSet attrs) {
  51. this(context, attrs, 0);
  52. }
  53. public ImportVoicePreference(Context context) {
  54. this(context, null);
  55. }
  56. public void setDescription(int resId) {
  57. callChangeListener(getContext().getString(resId));
  58. }
  59. @Override
  60. protected View onCreateDialogView() {
  61. View root = super.onCreateDialogView();
  62. mDictionaries = (Spinner)root.findViewById(R.id.dictionaries);
  63. return root;
  64. }
  65. @Override
  66. protected void onBindDialogView(View view) {
  67. super.onBindDialogView(view);
  68. File[] dictionaries = mRoot.listFiles(new FileFilter() {
  69. @Override
  70. public boolean accept(File file) {
  71. return !file.isDirectory() && file.getName().endsWith("_dict");
  72. }
  73. });
  74. if (dictionaries != null) {
  75. Arrays.sort(dictionaries);
  76. mDictionaries.setAdapter(new FileListAdapter((Activity) getContext(), dictionaries));
  77. }
  78. }
  79. @Override
  80. public void onClick(DialogInterface dialog, int which) {
  81. switch (which) {
  82. case DialogInterface.BUTTON_POSITIVE:
  83. new AsyncTask<Object,Object,File>() {
  84. @Override
  85. protected File doInBackground(Object... objects) {
  86. File source = (File)mDictionaries.getSelectedItem();
  87. if (source != null) {
  88. File destination = new File(CheckVoiceData.getDataPath(getContext()), source.getName());
  89. try {
  90. byte[] data = FileUtils.readBinary(source);
  91. FileUtils.write(destination, data);
  92. return source;
  93. } catch (IOException e) {
  94. }
  95. }
  96. return null;
  97. }
  98. @Override
  99. protected void onPostExecute(File file) {
  100. if (file != null) {
  101. final Intent intent = new Intent(DownloadVoiceData.BROADCAST_LANGUAGES_UPDATED);
  102. getContext().sendBroadcast(intent);
  103. }
  104. }
  105. }.execute();
  106. break;
  107. }
  108. super.onClick(dialog, which);
  109. }
  110. }