Debian Codegraph @

android/src/com/reecedunn/espeak/preference/ImportVoicePreference.java

raw symbols SCIP
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
17 package com.reecedunn.espeak.preference;
18
19 import android.app.Activity;
20 import android.app.DownloadManager;
21 import android.content.Context;
22 import android.content.DialogInterface;
23 import android.content.Intent;
24 import android.os.AsyncTask;
25 import android.os.Environment;
26 import android.preference.DialogPreference;
27 import android.util.AttributeSet;
28 import android.view.View;
29 import android.widget.Spinner;
30
31 import com.reecedunn.espeak.CheckVoiceData;
32 import com.reecedunn.espeak.DownloadVoiceData;
33 import com.reecedunn.espeak.FileListAdapter;
34 import com.reecedunn.espeak.FileUtils;
35 import com.reecedunn.espeak.R;
36
37 import java.io.File;
38 import java.io.FileFilter;
39 import java.io.FileInputStream;
40 import java.io.FileNotFoundException;
41 import java.io.IOException;
42 import java.util.Arrays;
43
44 public class ImportVoicePreference extends DialogPreference {
45 private File mRoot;
46 private Spinner mDictionaries;
47
48 public ImportVoicePreference(Context context, AttributeSet attrs, int defStyle) {
49 super(context, attrs, defStyle);
50 setDialogLayoutResource(R.layout.import_voice_preference);
51 setLayoutResource(R.layout.information_view);
52 setPositiveButtonText(android.R.string.ok);
53 setNegativeButtonText(android.R.string.cancel);
54
55 mRoot = Environment.getExternalStorageDirectory();
56 }
57
58 public ImportVoicePreference(Context context, AttributeSet attrs) {
59 this(context, attrs, 0);
60 }
61
62 public ImportVoicePreference(Context context) {
63 this(context, null);
64 }
65
66 public void setDescription(int resId) {
67 callChangeListener(getContext().getString(resId));
68 }
69
70 @Override
71 protected View onCreateDialogView() {
72 View root = super.onCreateDialogView();
73 mDictionaries = (Spinner)root.findViewById(R.id.dictionaries);
74 return root;
75 }
76
77 @Override
78 protected void onBindDialogView(View view) {
79 super.onBindDialogView(view);
80 File[] dictionaries = mRoot.listFiles(new FileFilter() {
81 @Override
82 public boolean accept(File file) {
83 return !file.isDirectory() && file.getName().endsWith("_dict");
84 }
85 });
86 if (dictionaries != null) {
87 Arrays.sort(dictionaries);
88 mDictionaries.setAdapter(new FileListAdapter((Activity) getContext(), dictionaries));
89 }
90 }
91
92 @Override
93 public void onClick(DialogInterface dialog, int which) {
94 switch (which) {
95 case DialogInterface.BUTTON_POSITIVE:
96 new AsyncTask<Object,Object,File>() {
97 @Override
98 protected File doInBackground(Object... objects) {
99 File source = (File)mDictionaries.getSelectedItem();
100 if (source != null) {
101 File destination = new File(CheckVoiceData.getDataPath(getContext()), source.getName());
102 try {
103 byte[] data = FileUtils.readBinary(source);
104 FileUtils.write(destination, data);
105 return source;
106 } catch (IOException e) {
107 }
108 }
109 return null;
110 }
111
112 @Override
113 protected void onPostExecute(File file) {
114 if (file != null) {
115 final Intent intent = new Intent(DownloadVoiceData.BROADCAST_LANGUAGES_UPDATED);
116 getContext().sendBroadcast(intent);
117 }
118 }
119 }.execute();
120 break;
121 }
122 super.onClick(dialog, which);
123 }
124 }