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
|
package org.codefirex.cfxweather;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.codefirex.cfxweather.ResourceMaps.ResInfo;
import org.codefirex.utils.WeatherInfo;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
public class WeatherProvider extends ContentProvider {
public static final String PACKAGE_NAME = "org.codefirex.cfxweather";
public static final String WEATHER_AUTH = PACKAGE_NAME + ".icons";
public static final String DATA_AUTH = PACKAGE_NAME + ".data";
public static final Uri ICON_URI = Uri.parse("content://" + WEATHER_AUTH
+ "/icons/#");
public static final Uri DATA_URI = Uri.parse("content://" + DATA_AUTH);
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
throw new RuntimeException("WeatherIconProvider.delete not supported");
}
@Override
public String getType(Uri uri) {
return WEATHER_AUTH;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
throw new RuntimeException("WeatherIconProvider.insert not supported");
}
@Override
public boolean onCreate() {
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
Object[] row;
if (uri.equals(DATA_URI)) {
MatrixCursor result1 = new MatrixCursor(projection);
WeatherInfo info = WeatherPrefs.getInfoFromPrefs(getContext());
Bundle b = new Bundle();
b.putParcelable(WeatherInfo.WEATHER_INFO_KEY, info);
b.putBoolean(WeatherInfo.WEATHER_STATE_KEY, WeatherPrefs.getEnabled(getContext()));
row = new Object[projection.length];
for (int i = 0; i < projection.length; i++) {
if (projection[i]
.compareToIgnoreCase(MediaStore.MediaColumns.DISPLAY_NAME) == 0) {
row[i] = "WeatherInfo";
} else if (projection[i]
.compareToIgnoreCase(MediaStore.MediaColumns.SIZE) == 0) {
row[i] = 0;
} else if (projection[i]
.compareToIgnoreCase(MediaStore.MediaColumns.DATA) == 0) {
row[i] = b;
} else if (projection[i]
.compareToIgnoreCase(MediaStore.MediaColumns.MIME_TYPE) == 0) {
row[i] = DATA_AUTH;
}
}
result1.addRow(row);
result1.setExtras(b);
return result1;
} else {
MatrixCursor result = new MatrixCursor(projection);
long fileSize = 0;
int index = Integer.parseInt((uri.getFragment()));
File tempFile = getIconFromAssets(index);
fileSize = tempFile.length();
String iconName = getIconName(index);
row = new Object[projection.length];
for (int i = 0; i < projection.length; i++) {
if (projection[i]
.compareToIgnoreCase(MediaStore.MediaColumns.DISPLAY_NAME) == 0) {
row[i] = iconName;
} else if (projection[i]
.compareToIgnoreCase(MediaStore.MediaColumns.SIZE) == 0) {
row[i] = fileSize;
} else if (projection[i]
.compareToIgnoreCase(MediaStore.MediaColumns.DATA) == 0) {
row[i] = tempFile;
} else if (projection[i]
.compareToIgnoreCase(MediaStore.MediaColumns.MIME_TYPE) == 0) {
row[i] = WEATHER_AUTH;
}
}
result.addRow(row);
return result;
}
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
throw new RuntimeException("WeatherIconProvider.update not supported");
}
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
throws FileNotFoundException {
int index = Integer.parseInt((uri.getFragment()));
File tempFile = getIconFromAssets(index);
return ParcelFileDescriptor.open(tempFile,
ParcelFileDescriptor.MODE_READ_ONLY);
}
private String getIconName(int conditionCode) {
if (conditionCode == -1 || conditionCode == 3200) {
conditionCode = 48;
}
ResInfo res = (ResInfo) ResourceMaps.weather_map.get(conditionCode);
return res.iconName + ".png";
}
static String getConditionText(Context context, int conditionCode) {
if (conditionCode == -1 || conditionCode == 3200) {
conditionCode = 48;
}
ResInfo res = (ResInfo) ResourceMaps.weather_map.get(conditionCode);
return context.getString(res.textRes);
}
private File getIconFromAssets(int index) {
if (index == -1 || index == 3200) {
index = 48;
}
String filename = getIconName(index);
File f = new File(getContext().getCacheDir() + "/" + filename);
InputStream is;
try {
if (!f.exists()) {
is = getContext().getAssets().open(filename);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
FileOutputStream fos = new FileOutputStream(f);
fos.write(buffer);
fos.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return f;
}
}
|