1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
package james.dsp.preference;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.preference.ListPreference;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Toast;
import james.dsp.R;
import james.dsp.activity.DSPManager;
import james.dsp.activity.JdspImpResToolbox;
import james.dsp.service.HeadsetService;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;
public class SummariedListPreferenceWithCustom extends ListPreference
{
public SummariedListPreferenceWithCustom(Context context, AttributeSet set)
{
super(context, set);
}
// Read file list from path
private static void getFileNameList(File path, String fileExt, ArrayList<String> fileList)
{
if (path.isDirectory())
{
File[] files = path.listFiles();
if (null == files) return;
for (File file : files) getFileNameList(file, fileExt, fileList);
}
else
{
String filePath = path.getAbsolutePath();
String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
if (fileName.toLowerCase(Locale.US).endsWith(fileExt))
fileList.add(fileName);
}
}
@Override
protected void onPrepareDialogBuilder(Builder builder)
{
try
{
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
Log.i(DSPManager.TAG, "External storage not mounted");
setEntries(new String[0]);
setEntryValues(new String[0]);
String tip = getContext().getResources().getString(R.string.text_ir_dir_isempty);
tip = String.format(tip, DSPManager.impulseResponsePath);
Toast.makeText(getContext(), tip, Toast.LENGTH_LONG).show();
super.onPrepareDialogBuilder(builder);
return;
}
final String kernelPath = DSPManager.impulseResponsePath;
File kernelFile = new File(kernelPath);
if (!kernelFile.exists())
{
Log.i(DSPManager.TAG, "Impulse response directory does not exists");
kernelFile.mkdirs();
kernelFile.mkdir();
}
ArrayList<String> kernelList = new ArrayList<>();
getFileNameList(kernelFile, ".irs", kernelList);
getFileNameList(kernelFile, ".wav", kernelList);
getFileNameList(kernelFile, ".flac", kernelList);
if (kernelList.isEmpty())
{
String tip = getContext().getResources().getString(R.string.text_ir_dir_isempty);
tip = String.format(tip, DSPManager.impulseResponsePath);
Toast.makeText(getContext(), tip, Toast.LENGTH_LONG).show();
}
else Collections.sort(kernelList);
final String[] kernelArray = new String[kernelList.size()];
final String[] arrayValue = new String[kernelList.size()];
for (int i = 0; i < kernelList.size(); i++)
{
kernelArray[i] = kernelList.get(i);
arrayValue[i] = kernelPath + kernelList.get(i);
}
setEntries(kernelArray);
setEntryValues(arrayValue);
super.onPrepareDialogBuilder(builder);
}
catch (Exception e)
{
setEntries(new String[0]);
setEntryValues(new String[0]);
String tip = getContext().getResources().getString(R.string.text_ir_dir_isempty);
tip = String.format(tip, DSPManager.impulseResponsePath);
Toast.makeText(getContext(), tip, Toast.LENGTH_LONG).show();
super.onPrepareDialogBuilder(builder);
}
}
@Override
public void setValue(String value)
{
super.setValue(value);
try
{
CharSequence[] entries = getEntries();
CharSequence[] entryValues = getEntryValues();
if (entries == null || entryValues == null)
{
if (value == null)
{
setSummary("");
return;
}
if (value.isEmpty())
{
setSummary("");
return;
}
if (value.contains("/"))
{
String fileName = value.substring(value.lastIndexOf("/") + 1);
setSummary(fileName);
return;
}
setSummary(value);
return;
}
if(getKey().equals("dsp.convolver.resampler"))
{
if(HeadsetService.dspModuleSamplingRate > 0)
{
for (int i = 0; i < entryValues.length; i++)
{
if (entryValues[i].equals(value))
{
Runnable runner = new ResamplerThread(DSPManager.impulseResponsePath, entries[i].toString(), HeadsetService.dspModuleSamplingRate);
new Thread(runner).start();
break;
}
}
}
else
Toast.makeText(DSPManager.actUi, DSPManager.actUi.getString(R.string.resamplererror), Toast.LENGTH_LONG).show();
}
else
{
for (int i = 0; i < entryValues.length; i++)
{
if (entryValues[i].equals(value))
{
setSummary(entries[i]);
break;
}
}
}
}
catch (Exception e)
{
setSummary("");
}
}
}
class ResamplerThread implements Runnable {
private String path;
private String filename;
private int tarSmpRate;
ResamplerThread(String p, String f, int s) {
path = p;
filename = f;
tarSmpRate = s;
}
public void run() {
final String finalName = JdspImpResToolbox.OfflineAudioResample(path, filename, tarSmpRate);
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
File f = new File(finalName);
if(f.exists() && !f.isDirectory())
Toast.makeText(DSPManager.actUi, DSPManager.actUi.getString(R.string.resamplerstr, tarSmpRate, finalName), Toast.LENGTH_LONG).show();
}
});
}
}
|