1 |
|
|
2 |
|
package pl.magot.vetch.ancal.reminder; |
3 |
|
|
4 |
|
|
5 |
|
import java.io.File; |
6 |
|
import java.io.FileNotFoundException; |
7 |
|
import java.io.FileOutputStream; |
8 |
|
import java.io.IOException; |
9 |
|
import java.io.InputStream; |
10 |
|
import android.content.Context; |
11 |
|
import android.net.Uri; |
12 |
|
import android.content.res.AssetManager; |
13 |
|
|
14 |
|
|
|
|
| 0% |
Uncovered Elements: 62 (62) |
Complexity: 18 |
Complexity Density: 0,46 |
|
15 |
|
public class AlarmSound |
16 |
|
{ |
17 |
|
|
18 |
|
private Context context = null; |
19 |
|
|
20 |
|
|
21 |
|
private final String strAlarmFileName = "ancalalarm.mp3"; |
22 |
|
private String strFilesPath = ""; |
23 |
|
private File alarmFile = null; |
24 |
|
|
25 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0,4 |
|
26 |
0
|
public AlarmSound(Context context)... |
27 |
|
{ |
28 |
0
|
this.context = context; |
29 |
0
|
strFilesPath = String.format("//data//data//%s//files//", this.context.getPackageName()); |
30 |
0
|
alarmFile = new File(strFilesPath + strAlarmFileName); |
31 |
0
|
if (!alarmFile.exists()) |
32 |
0
|
copyAlarmSoundToFileSystem(); |
33 |
|
} |
34 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 4 |
Complexity Density: 0,5 |
|
35 |
0
|
private int getStreamFileLength(InputStream stream)... |
36 |
|
{ |
37 |
0
|
if (stream != null) |
38 |
|
{ |
39 |
0
|
int iSize = 0; |
40 |
0
|
try |
41 |
|
{ |
42 |
0
|
while (stream.read() != -1) |
43 |
0
|
iSize ++; |
44 |
0
|
return iSize; |
45 |
|
} catch (IOException e) { |
46 |
0
|
return -1; |
47 |
|
} |
48 |
|
} |
49 |
0
|
return -1; |
50 |
|
} |
51 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 5 |
Complexity Density: 0,56 |
|
52 |
0
|
private boolean copyBufferToFile(byte[] buffer, int iSize, String sTargetFileName)... |
53 |
|
{ |
54 |
0
|
if (buffer != null) |
55 |
|
{ |
56 |
0
|
try |
57 |
|
{ |
58 |
0
|
FileOutputStream fileOut = context.openFileOutput(sTargetFileName, Context.MODE_WORLD_READABLE); |
59 |
0
|
if (fileOut != null) |
60 |
|
{ |
61 |
0
|
fileOut.write(buffer, 0, iSize); |
62 |
0
|
fileOut.flush(); |
63 |
0
|
fileOut.close(); |
64 |
0
|
return true; |
65 |
|
} |
66 |
|
} catch (FileNotFoundException e1) { |
67 |
|
} catch (IOException e) { |
68 |
|
} |
69 |
|
} |
70 |
0
|
return false; |
71 |
|
} |
72 |
|
|
|
|
| 0% |
Uncovered Elements: 20 (20) |
Complexity: 5 |
Complexity Density: 0,36 |
|
73 |
0
|
private boolean copyAlarmSoundToFileSystem()... |
74 |
|
{ |
75 |
0
|
boolean bResult = false; |
76 |
0
|
AssetManager assets = context.getAssets(); |
77 |
0
|
if (assets != null) |
78 |
|
{ |
79 |
0
|
try |
80 |
|
{ |
81 |
0
|
InputStream stream = assets.open(strAlarmFileName); |
82 |
0
|
if (stream != null) |
83 |
|
{ |
84 |
0
|
final int iStreamFileSize = getStreamFileLength(stream); |
85 |
0
|
if (iStreamFileSize != -1) |
86 |
|
{ |
87 |
|
|
88 |
0
|
byte[] buffer = new byte[iStreamFileSize]; |
89 |
|
|
90 |
0
|
stream.read(buffer, 0, iStreamFileSize); |
91 |
|
|
92 |
0
|
bResult = copyBufferToFile(buffer, iStreamFileSize, strAlarmFileName); |
93 |
|
} |
94 |
0
|
stream.close(); |
95 |
|
} |
96 |
|
} catch (IOException e) { |
97 |
|
} |
98 |
0
|
assets.close(); |
99 |
|
} |
100 |
0
|
return bResult; |
101 |
|
} |
102 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
103 |
0
|
public Uri getUri()... |
104 |
|
{ |
105 |
0
|
if (alarmFile.exists()) |
106 |
|
{ |
107 |
0
|
return Uri.fromFile(alarmFile); |
108 |
|
} else { |
109 |
0
|
return null; |
110 |
|
} |
111 |
|
} |
112 |
|
|
113 |
|
} |