19
19
package com .osfans .trime .data ;
20
20
21
21
import android .content .Context ;
22
- import android .content .res .AssetManager ;
23
22
import android .content .res .Resources ;
24
23
import android .graphics .Bitmap ;
25
24
import android .graphics .BitmapFactory ;
39
38
import com .osfans .trime .ime .keyboard .Key ;
40
39
import com .osfans .trime .ime .keyboard .Sound ;
41
40
import com .osfans .trime .ime .symbol .TabManager ;
42
- import com .osfans .trime .util .AppVersionUtils ;
43
41
import com .osfans .trime .util .ConfigGetter ;
44
42
import java .io .File ;
45
43
import java .io .FileInputStream ;
46
44
import java .io .FileOutputStream ;
47
45
import java .io .FilenameFilter ;
48
- import java .io .IOException ;
49
46
import java .io .InputStream ;
50
47
import java .io .OutputStream ;
51
48
import java .util .ArrayList ;
@@ -64,7 +61,6 @@ public class Config {
64
61
// private static final String TAG = "Config";
65
62
66
63
private static Config self = null ;
67
- private static AssetManager assetManager = null ;
68
64
69
65
private static final AppPrefs appPrefs = AppPrefs .defaultInstance ();
70
66
@@ -99,12 +95,12 @@ public Config(@NonNull Context context, boolean skipDeploy) {
99
95
"\t <TrimeInit>\t " + Thread .currentThread ().getStackTrace ()[2 ].getMethodName () + "\t " ;
100
96
Timber .d (methodName );
101
97
self = this ;
102
- assetManager = context .getAssets ();
103
98
themeName = appPrefs .getLooks ().getSelectedTheme ();
104
99
soundPackageName = appPrefs .getKeyboard ().getSoundPackage ();
105
100
106
- Timber .d (methodName + "prepareRime" );
107
- prepareRime (context );
101
+ Timber .d (methodName + "sync" );
102
+ DataManager .sync ();
103
+ Rime .get (context , !DataManager .INSTANCE .getSharedDataDir ().exists ());
108
104
109
105
// 正常逻辑不应该部署全部主题,init()方法已经做过当前主题的部署
110
106
// Timber.d(methodName + "deployTheme");
@@ -173,30 +169,6 @@ public String getSoundPackage() {
173
169
return soundPackageName ;
174
170
}
175
171
176
- public void prepareRime (Context context ) {
177
- String methodName =
178
- "\t <TrimeInit>\t " + Thread .currentThread ().getStackTrace ()[2 ].getMethodName () + "\t " ;
179
- Timber .d (methodName );
180
- boolean isExist = new File (sharedDataDir ).exists ();
181
- boolean isOverwrite = AppVersionUtils .INSTANCE .isDifferentVersion (appPrefs );
182
- String defaultFile = "trime.yaml" ;
183
- Timber .d (methodName + "copy" );
184
- copyFileOrDir ("" , isOverwrite );
185
- // 缺失导致获取方案列表为空
186
- Timber .d (methodName + "copy default.custom.yaml" );
187
- final String defaultCustom = "default.custom.yaml" ;
188
- if (!new File (userDataDir , defaultCustom ).exists ()) {
189
- try {
190
- new File (userDataDir , defaultCustom ).createNewFile ();
191
- } catch (IOException e ) {
192
- e .printStackTrace ();
193
- }
194
- }
195
- Timber .d (methodName + "Rime.get" );
196
- Rime .get (context , !isExist ); // 覆蓋時不強制部署
197
- Timber .d (methodName + "finish" );
198
- }
199
-
200
172
public static String [] getThemeKeys (boolean isUser ) {
201
173
File d = new File (isUser ? userDataDir : sharedDataDir );
202
174
FilenameFilter trimeFilter = (dir , filename ) -> filename .endsWith ("trime.yaml" );
@@ -225,49 +197,6 @@ public static String[] getYamlFileNames(String[] keys) {
225
197
return names ;
226
198
}
227
199
228
- public boolean copyFileOrDir (String path , boolean overwrite ) {
229
- try {
230
- final String assetPath = new File (RIME , path ).getPath ();
231
- final String [] assets = assetManager .list (assetPath );
232
- if (assets .length == 0 ) {
233
- // Files
234
- copyFile (path , overwrite );
235
- } else {
236
- // Dirs
237
- final File dir = new File (sharedDataDir , path );
238
- if (!dir .exists ()) // noinspection ResultOfMethodCallIgnored
239
- dir .mkdir ();
240
- for (String asset : assets ) {
241
- final String subPath = new File (path , asset ).getPath ();
242
- copyFileOrDir (subPath , overwrite );
243
- }
244
- }
245
- } catch (Exception e ) {
246
- e .printStackTrace ();
247
- return false ;
248
- }
249
- return true ;
250
- }
251
-
252
- private void copyFile (String fileName , boolean overwrite ) {
253
- if (fileName == null ) return ;
254
-
255
- final String targetFileName = new File (sharedDataDir , fileName ).getPath ();
256
- if (new File (targetFileName ).exists () && !overwrite ) return ;
257
- final String sourceFileName = new File (RIME , fileName ).getPath ();
258
- try (InputStream in = assetManager .open (sourceFileName );
259
- final FileOutputStream out = new FileOutputStream (targetFileName )) {
260
- final byte [] buffer = new byte [1024 ];
261
- int read ;
262
- while ((read = in .read (buffer )) != -1 ) {
263
- out .write (buffer , 0 , read );
264
- }
265
- out .flush ();
266
- } catch (IOException e ) {
267
- e .printStackTrace ();
268
- }
269
- }
270
-
271
200
private void deployTheme () {
272
201
if (userDataDir .contentEquals (sharedDataDir )) return ; // 相同文件夾不部署主題
273
202
final String [] configs = getThemeKeys (false );
0 commit comments