|
| 1 | +package com.tns; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.DataOutputStream; |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileFilter; |
| 7 | +import java.io.FileInputStream; |
| 8 | +import java.io.FileNotFoundException; |
| 9 | +import java.io.FileOutputStream; |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStreamReader; |
| 12 | +import java.util.ArrayList; |
| 13 | + |
| 14 | +import android.content.Context; |
| 15 | +import android.content.pm.ApplicationInfo; |
| 16 | +import android.content.pm.PackageManager.NameNotFoundException; |
| 17 | +import android.util.Log; |
| 18 | + |
| 19 | +public class NativeScriptSyncHelper |
| 20 | +{ |
| 21 | + private static String SYNC_ROOT_SOURCE_DIR = "/data/local/tmp/"; |
| 22 | + private static final String SYNC_SOURCE_DIR = "/sync/"; |
| 23 | + private static final String FULL_SYNC_SOURCE_DIR = "/fullsync/"; |
| 24 | + private static final String REMOVED_SYNC_SOURCE_DIR = "/removedsync/"; |
| 25 | + private static final String SYNC_THUMB_FILE = "syncThumb"; |
| 26 | + private static final String COMMAND_FULL_SYNC = "fullsync"; |
| 27 | + private static final String COMMAND_SYNC = "sync"; |
| 28 | + private static final String COMMAND_SYNC_WITH_REMOVED = "sync-with-removed"; |
| 29 | + |
| 30 | + public static void sync(Context context) |
| 31 | + { |
| 32 | + boolean shouldExecuteSync = getShouldExecuteSync(context); |
| 33 | + |
| 34 | + if (!shouldExecuteSync) |
| 35 | + { |
| 36 | + if (Platform.IsLogEnabled) |
| 37 | + { |
| 38 | + Log.d(Platform.DEFAULT_LOG_TAG, "Sync is not enabled."); |
| 39 | + } |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | +// String syncThumbFilePath = SYNC_ROOT_SOURCE_DIR + context.getPackageName() + SYNC_THUMB_FILE; |
| 44 | +// String syncData = getSyncThumb(syncThumbFilePath); |
| 45 | +// if (syncData == null) |
| 46 | +// { |
| 47 | +// return; |
| 48 | +// } |
| 49 | +// |
| 50 | +// String[] syncCommandAndThumb = syncData.split(""); |
| 51 | +// String syncCommand = syncCommandAndThumb[0]; |
| 52 | +// String syncThumb = syncCommandAndThumb[1]; |
| 53 | +// if (syncThumb == null) |
| 54 | +// { |
| 55 | +// return; |
| 56 | +// } |
| 57 | +// |
| 58 | +// String oldSyncThumbFilePath = context.getFilesDir() + "syncThumb"; |
| 59 | +// String oldSyncThumb = getSyncThumb(oldSyncThumbFilePath); |
| 60 | +// if (oldSyncThumb.equalsIgnoreCase(syncThumb)) |
| 61 | +// { |
| 62 | +// return; |
| 63 | +// } |
| 64 | + |
| 65 | + String syncPath = SYNC_ROOT_SOURCE_DIR + context.getPackageName() + SYNC_SOURCE_DIR; |
| 66 | + String fullSyncPath = SYNC_ROOT_SOURCE_DIR + context.getPackageName() + FULL_SYNC_SOURCE_DIR; |
| 67 | + String removedSyncPath = SYNC_ROOT_SOURCE_DIR + context.getPackageName() + REMOVED_SYNC_SOURCE_DIR; |
| 68 | + |
| 69 | + if (Platform.IsLogEnabled) |
| 70 | + { |
| 71 | + Log.d(Platform.DEFAULT_LOG_TAG, "Sync is enabled:"); |
| 72 | + Log.d(Platform.DEFAULT_LOG_TAG, "Sync path : " + syncPath); |
| 73 | + Log.d(Platform.DEFAULT_LOG_TAG, "Full sync path : " + fullSyncPath); |
| 74 | + Log.d(Platform.DEFAULT_LOG_TAG, "Removed files sync path: " + removedSyncPath); |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + File fullSyncDir = new File(fullSyncPath); |
| 79 | + if (fullSyncDir.exists()) |
| 80 | + { |
| 81 | + executeFullSync(context, fullSyncDir); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + File syncDir = new File(syncPath); |
| 86 | + if (syncDir.exists()) |
| 87 | + { |
| 88 | + executePartialSync(context, syncDir); |
| 89 | + } |
| 90 | + |
| 91 | + File removedSyncDir = new File(removedSyncPath); |
| 92 | + if (removedSyncDir.exists()) |
| 93 | + { |
| 94 | + executeRemovedSync(context, removedSyncDir); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private static boolean getShouldExecuteSync(Context context) |
| 99 | + { |
| 100 | + int flags; |
| 101 | + boolean shouldExecuteSync = false; |
| 102 | + try |
| 103 | + { |
| 104 | + flags = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).applicationInfo.flags; |
| 105 | + shouldExecuteSync = ((flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0); |
| 106 | + } |
| 107 | + catch (NameNotFoundException e) |
| 108 | + { |
| 109 | + e.printStackTrace(); |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + return shouldExecuteSync; |
| 114 | + } |
| 115 | + |
| 116 | + final static FileFilter deletingFilesFilter = new FileFilter() |
| 117 | + { |
| 118 | + @Override |
| 119 | + public boolean accept(File pathname) |
| 120 | + { |
| 121 | + if (pathname.isDirectory()) |
| 122 | + { |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | + boolean success = pathname.delete(); |
| 127 | + if (!success) |
| 128 | + { |
| 129 | + Log.e(Platform.DEFAULT_LOG_TAG, "Syncing: file not deleted: " + pathname.getAbsolutePath().toString()); |
| 130 | + } |
| 131 | + return false; |
| 132 | + } |
| 133 | + }; |
| 134 | + |
| 135 | + private static void deleteDir(File directory) |
| 136 | + { |
| 137 | + File[] subDirectories = directory.listFiles(deletingFilesFilter); |
| 138 | + if (subDirectories != null) |
| 139 | + { |
| 140 | + for (int i = 0; i < subDirectories.length; i++) |
| 141 | + { |
| 142 | + File subDir = subDirectories[i]; |
| 143 | + deleteDir(subDir); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + boolean success = directory.delete(); |
| 148 | + if (!success && directory.exists()) |
| 149 | + { |
| 150 | + Log.e(Platform.DEFAULT_LOG_TAG, "Syncing: directory not deleted: " + directory.getAbsolutePath().toString()); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private static void moveFiles(File sourceDir, String sourceRootAbsolutePath, String targetRootAbsolutePath) |
| 155 | + { |
| 156 | + File[] files = sourceDir.listFiles(); |
| 157 | + |
| 158 | + if (files != null) |
| 159 | + { |
| 160 | + for (int i = 0; i < files.length; i++) |
| 161 | + { |
| 162 | + File file = files[i]; |
| 163 | + if (file.isFile()) |
| 164 | + { |
| 165 | + if (Platform.IsLogEnabled) |
| 166 | + { |
| 167 | + Log.d(Platform.DEFAULT_LOG_TAG, "Syncing: " + file.getAbsolutePath().toString()); |
| 168 | + } |
| 169 | + |
| 170 | + String targetFilePath = file.getAbsolutePath().replace(sourceRootAbsolutePath, targetRootAbsolutePath); |
| 171 | + File targetFileDir = new File(targetFilePath); |
| 172 | + |
| 173 | + File targetParent = targetFileDir.getParentFile(); |
| 174 | + if (targetParent != null) |
| 175 | + { |
| 176 | + targetParent.mkdirs(); |
| 177 | + } |
| 178 | + |
| 179 | + boolean success = copyFile(file.getAbsolutePath(), targetFilePath); |
| 180 | + if (!success) |
| 181 | + { |
| 182 | + Log.e(Platform.DEFAULT_LOG_TAG, "Sync failed: " + file.getAbsolutePath().toString()); |
| 183 | + } |
| 184 | + } |
| 185 | + else |
| 186 | + { |
| 187 | + moveFiles(file, sourceRootAbsolutePath, targetRootAbsolutePath); |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + // this removes only the app directory from the device to preserve |
| 194 | + // any existing files in /files directory on the device |
| 195 | + private static void executeFullSync(Context context, final File sourceDir) |
| 196 | + { |
| 197 | + String appPath = context.getFilesDir().getAbsolutePath() + "/app"; |
| 198 | + final File appDir = new File(appPath); |
| 199 | + |
| 200 | + if (appDir.exists()) |
| 201 | + { |
| 202 | + deleteDir(appDir); |
| 203 | + moveFiles(sourceDir, sourceDir.getAbsolutePath(), appDir.getAbsolutePath()); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + private static void executePartialSync(Context context, File sourceDir) |
| 208 | + { |
| 209 | + String appPath = context.getFilesDir().getAbsolutePath() + "/app"; |
| 210 | + final File appDir = new File(appPath); |
| 211 | + |
| 212 | + if (appDir.exists()) |
| 213 | + { |
| 214 | + moveFiles(sourceDir, sourceDir.getAbsolutePath(), appDir.getAbsolutePath()); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + private static void deleteRemovedFiles(File sourceDir, String sourceRootAbsolutePath, String targetRootAbsolutePath) |
| 219 | + { |
| 220 | + File[] files = sourceDir.listFiles(); |
| 221 | + |
| 222 | + if (files != null) |
| 223 | + { |
| 224 | + for (int i = 0; i < files.length; i++) |
| 225 | + { |
| 226 | + File file = files[i]; |
| 227 | + if (file.isFile()) |
| 228 | + { |
| 229 | + if (Platform.IsLogEnabled) |
| 230 | + { |
| 231 | + Log.d(Platform.DEFAULT_LOG_TAG, "Syncing removed file: " + file.getAbsolutePath().toString()); |
| 232 | + } |
| 233 | + |
| 234 | + String targetFilePath = file.getAbsolutePath().replace(sourceRootAbsolutePath, targetRootAbsolutePath); |
| 235 | + File targetFile = new File(targetFilePath); |
| 236 | + targetFile.delete(); |
| 237 | + } |
| 238 | + else |
| 239 | + { |
| 240 | + deleteRemovedFiles(file, sourceRootAbsolutePath, targetRootAbsolutePath); |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + private static void executeRemovedSync(final Context context, final File sourceDir) |
| 247 | + { |
| 248 | + String appPath = context.getFilesDir().getAbsolutePath() + "/app"; |
| 249 | + deleteRemovedFiles(sourceDir, sourceDir.getAbsolutePath(), appPath); |
| 250 | + } |
| 251 | + |
| 252 | + private static boolean copyFile(String sourceFile, String destinationFile) |
| 253 | + { |
| 254 | + FileInputStream fis = null; |
| 255 | + FileOutputStream fos = null; |
| 256 | + |
| 257 | + try |
| 258 | + { |
| 259 | + fis = new FileInputStream(sourceFile); |
| 260 | + fos = new FileOutputStream(destinationFile, false); |
| 261 | + |
| 262 | + byte[] buffer = new byte[4096]; |
| 263 | + int read = 0; |
| 264 | + |
| 265 | + while ((read = fis.read(buffer)) != -1) |
| 266 | + { |
| 267 | + fos.write(buffer, 0, read); |
| 268 | + } |
| 269 | + } |
| 270 | + catch (FileNotFoundException e) |
| 271 | + { |
| 272 | + Log.e(Platform.DEFAULT_LOG_TAG, "Error copying file " + sourceFile); |
| 273 | + e.printStackTrace(); |
| 274 | + return false; |
| 275 | + } |
| 276 | + catch (IOException e) |
| 277 | + { |
| 278 | + Log.e(Platform.DEFAULT_LOG_TAG, "Error copying file " + sourceFile); |
| 279 | + e.printStackTrace(); |
| 280 | + return false; |
| 281 | + } |
| 282 | + finally |
| 283 | + { |
| 284 | + try |
| 285 | + { |
| 286 | + if (fis != null) |
| 287 | + { |
| 288 | + fis.close(); |
| 289 | + } |
| 290 | + if (fos != null) |
| 291 | + { |
| 292 | + fos.close(); |
| 293 | + } |
| 294 | + } |
| 295 | + catch (IOException e) |
| 296 | + { |
| 297 | + } |
| 298 | + } |
| 299 | + |
| 300 | + return true; |
| 301 | + } |
| 302 | + |
| 303 | +// private static String getSyncThumb(String syncThumbFilePath) |
| 304 | +// { |
| 305 | +// try |
| 306 | +// { |
| 307 | +// File syncThumbFile = new File(syncThumbFilePath); |
| 308 | +// if (syncThumbFile.exists()) |
| 309 | +// { |
| 310 | +// return null; |
| 311 | +// } |
| 312 | +// |
| 313 | +// FileInputStream in = new FileInputStream(syncThumbFile); |
| 314 | +// BufferedReader reader = new BufferedReader(new InputStreamReader(in)); |
| 315 | +// String syncThumb = reader.readLine(); |
| 316 | +// reader.close(); |
| 317 | +// in.close(); |
| 318 | +// return syncThumb; |
| 319 | +// } |
| 320 | +// catch (FileNotFoundException e) |
| 321 | +// { |
| 322 | +// Log.e(Platform.DEFAULT_LOG_TAG, "Error while reading sync command"); |
| 323 | +// e.printStackTrace(); |
| 324 | +// } |
| 325 | +// catch (IOException e) |
| 326 | +// { |
| 327 | +// Log.e(Platform.DEFAULT_LOG_TAG, "Error while reading sync command"); |
| 328 | +// e.printStackTrace(); |
| 329 | +// } |
| 330 | +// |
| 331 | +// return null; |
| 332 | +// } |
| 333 | +} |
0 commit comments