From 2e83cbb216f8b6cdaf17b3d6d8cbc499b464a056 Mon Sep 17 00:00:00 2001 From: CiriousJoker Date: Sun, 5 Mar 2017 21:01:12 +0100 Subject: [PATCH] Updated in version 0.9.2 --- MainActivity.html | 6 ++--- js/AlarmManager.js | 11 +++++---- js/Helper.js | 31 +++++++++++++++++++++++++ js/MainActivity.js | 10 +++++---- js/WallpaperManager.js | 51 +++++++++++++++++++++++++----------------- manifest.json | 8 +++---- 6 files changed, 81 insertions(+), 36 deletions(-) diff --git a/MainActivity.html b/MainActivity.html index 9498fd4..845efcd 100644 --- a/MainActivity.html +++ b/MainActivity.html @@ -25,7 +25,7 @@
-

ShufflePaper 0.9.0

+

ShufflePaper 0.9.2

Choose a folder with your backgroud images. These will then be set as your wallpaper according to your preferences below. @@ -34,7 +34,7 @@

ShufflePaper 0.9.0

-
+
-
diff --git a/js/AlarmManager.js b/js/AlarmManager.js index 705f3b8..9616e9f 100644 --- a/js/AlarmManager.js +++ b/js/AlarmManager.js @@ -1,12 +1,15 @@ -function ALARM() {} +function AlarmManager() {} +AlarmManager.ID = function() {}; +AlarmManager.INTERVAL = function() {}; + + // Used as an identifier for the alarm used // to change the wallpaper every x minutes -ALARM.WALLPAPER_INTERVAL = "wallpaper_change_interval"; +AlarmManager.ID.Wallpaper = "wallpaper_change_interval"; +AlarmManager.INTERVAL.Wallpaper = 15; -function AlarmManager() {} - AlarmManager.set = function(id, interval) { chrome.alarms.create(id, { delayInMinutes: interval, diff --git a/js/Helper.js b/js/Helper.js index b4458d5..052a3a3 100644 --- a/js/Helper.js +++ b/js/Helper.js @@ -29,4 +29,35 @@ function getRoot(callback, return_anyway) { }, // Flag to return even if the result would be 'undefined' true); +} + +function getFormattedPath(dir, callback) { + chrome.fileSystem.getDisplayPath(dir, function(path) { + // eg. /special/drive-[SOME ID]/root/ + var regIsMyDrive = new RegExp('\/special\/drive-\\w*\/root\/'); + + // eg. /special/drive-[SOME ID]/other/ + var regIsShared = new RegExp('\/special\/drive-\\w*\/other\/'); + + // eg. /media/removable/My Usb/subfolder + var regIsExternal = new RegExp('\/media\/removable'); + + // eg /provided/[SOME ID]:chrome-extension_[SOME ID]_[SOME STORAGE NUMBER]:Persistent::::::::::::::chrome_extension:::::::::::::::[MOUNTED FOLDER NAME]:[SOME ID]/subfolders + var regIsProvided = new RegExp('\/provided\/.*::::::::::::::chrome_extension:::::::::::::::'); + + if (regIsMyDrive.test(path)) { + callback(path.replace(regIsMyDrive, "~/My Drive/")); + } else if (regIsShared.test(path)) { + callback(path.replace(regIsShared, "~/Shared/")); + } else if (regIsExternal.test(path)) { + callback(path.replace(regIsExternal, "~")); + } else if (regIsProvided.test(path)) { + var FormattedStart = path.replace(regIsProvided, "~/"); // Returns [MOUNTED FOLDER NAME]:[SOME ID]/subfolders + var StorageName = FormattedStart.substring(0, FormattedStart.lastIndexOf(":")); // Returns [MOUNTED FOLDER NAME] + var SubPath = FormattedStart.replace(new RegExp('~\/[^\/]*'), ""); // Returns /subfolders + callback(StorageName + SubPath); + } else { + callback(path); + } + }); } \ No newline at end of file diff --git a/js/MainActivity.js b/js/MainActivity.js index 1c796e9..fb94eb4 100644 --- a/js/MainActivity.js +++ b/js/MainActivity.js @@ -54,9 +54,9 @@ $(document).ready(function() { prefs.apply(); if(!this.checked) { - AlarmManager.clear(ALARM.WALLPAPER_INTERVAL); + AlarmManager.clear(AlarmManager.ID.Wallpaper); } else { - AlarmManager.set(ALARM.WALLPAPER_INTERVAL, 0.1); + AlarmManager.set(AlarmManager.ID.Wallpaper, AlarmManager.INTERVAL.Wallpaper); } }); }); @@ -85,10 +85,12 @@ function loadDirPath() { getRoot(function(rootDir) { if(!isdef(rootDir)) { $('#tv_chosen_folder').html("Please speficy a folder keyboard_arrow_right"); + $('#tv_chosen_folder').fadeIn(); } else { - chrome.fileSystem.getDisplayPath(rootDir, function(path) { + getFormattedPath(rootDir, function(path) { $('#tv_chosen_folder').html(path); - }); + $('#tv_chosen_folder').fadeIn(); + }); } }, // flag to return even if the result would be 'undefined' diff --git a/js/WallpaperManager.js b/js/WallpaperManager.js index 2c164aa..24a18a2 100644 --- a/js/WallpaperManager.js +++ b/js/WallpaperManager.js @@ -7,22 +7,27 @@ function loadNextWallpaper() { getNextId(_currentId, numbertotal, function(nextId) { readFolder(rootDir, function(fileEntry, number) { if(number == nextId) { - console.log("New wallpaper id: " + nextId); // Read the file as an arraybuffer readFileAsArrayBuffer(fileEntry, function(buf) { - // Set the arraybuffer as a wallpaper - chrome.wallpaper.setWallpaper( - { - 'data': buf.target.result, - 'filename': fileEntry.name, - 'layout': 'CENTER_CROPPED' - }, function() { + var regCheckFileExtension = new RegExp('(.jpg|.jpeg|.jfif|.png)', 'i'); + if(regCheckFileExtension.test(fileEntry.name) && isdef(buf)) { + // Set the arraybuffer as a wallpaper console.log("New wallpaper: " + fileEntry.name); - - // Store the new current_id as a SharedPreference + chrome.wallpaper.setWallpaper( + { + 'data': buf.target.result, + 'filename': fileEntry.name, + 'layout': 'CENTER_CROPPED' + }, function() { + // Store the new current_id as a SharedPreference + prefs.set(Key.current_file_id, nextId); + prefs.apply(); + }); + } else { + // Store the new current_id as a SharedPreference so that it doesn't try the same file over and over prefs.set(Key.current_file_id, nextId); prefs.apply(); - }); + } }); } }); @@ -37,7 +42,7 @@ function loadNextWallpaper() { // Pick the next file id based on the user's preferences function getNextId(currentId, filecount, callback) { prefs.get(Key.choose_random, function(is_random) { - if(is_random) { + if(isdef(is_random) && is_random === true) { callback(randomMinMax(0, filecount - 1)); } else { if(!isdef(currentId)) { @@ -50,18 +55,24 @@ function getNextId(currentId, filecount, callback) { callback(newId); } } - }); + }, + // optional flag to return even if the result would be "undefined" + true); } // Returns an ArrayBuffer with the file's content function readFileAsArrayBuffer(fileEntry, callback) { - fileEntry.file(function(file) { - var fileReader = new FileReader(); - fileReader.onloadend = function(arybf) { - callback(arybf); - }; - fileReader.readAsArrayBuffer(file); - }); + if(fileEntry.isFile) { + fileEntry.file(function(file) { + var fileReader = new FileReader(); + fileReader.onloadend = function(arybf) { + callback(arybf); + }; + fileReader.readAsArrayBuffer(file); + }); + } else { + callback(); + } } // Returns the file count of the folder diff --git a/manifest.json b/manifest.json index 0e518c0..1dce040 100644 --- a/manifest.json +++ b/manifest.json @@ -1,10 +1,10 @@ { "manifest_version": 2, "name": "ShufflePaper", - "version": "0.9.0", + "short_name": "ShufflePaper", + "version": "0.9.2", "description": "Choose a folder and have your Chromebook automatically shuffle through the wallpapers in it!", "minimum_chrome_version": "38", - "icons": { "16": "assets/ic_launcher_16.png", "48": "assets/ic_launcher_48.png", @@ -12,13 +12,11 @@ "512": "assets/ic_launcher_512.png" }, "permissions": [ - "notifications", "wallpaper", "alarms", "storage", { "fileSystem": [ - "write", "retainEntries", "directory" ] @@ -36,4 +34,4 @@ } }, "offline_enabled": true -} +} \ No newline at end of file