User Tools

Site Tools


script_autosetupappfreeze

**This is an old revision of the document!** ----

A PCRE internal error occured. This might be caused by a faulty plugin

====== About the script ====== * Purpose : this script automatically sets up app freezing on any action you want. * Author : cdfa * Initial request: https://plus.google.com/+JaroslavGrochal/posts/BDKDMrTMfxt * GitHub: https://github.com/cdfa/AutoSetupLongTapFreeze * Version: 1.7 ====== How to use the script ====== Run script in any container to run setup freezing and unfreezing actions for all items. The action is configurable at the top of the script. You can setup autosync in the setup, or you can sync manually from any item put in the container by running the script with "sync" (without the") as data, or from anywhere if you put the container id directly behind it, so like (sync1). You can sync items separately from the items menu and choosing Sync from the menu. If the menu is opened in any other way then is specified at the top of the script extra items won't be there. Run the script from the container again to uninstall (this won't clear events you manually set to sync the container). Run the script from the lightning menu to uninstall from every container. Please report all issues at the GitHub page. ====== Changelog ====== * Multithreading! LL won't get stuck anymore when executing a root command! ====== Script code ====== <sxh javascript> var blackList = ["net.pierrox.lightning_launcher_extreme"] , whiteList = [] // for Sources see: http://www.lightninglauncher.com/scripting/reference/api/reference/net/pierrox/lightning_launcher/script/api/Event.html // for Event see http://www.lightninglauncher.com/scripting/reference/api/reference/net/pierrox/lightning_launcher/script/api/PropertySet.html // you may only have to set 1 in the future. , freezeSource = "I_LONG_CLICK" , freezeEvent = "i.longTap" , menuSource = "I_SWIPE_UP" , menuEvent = "i.swipeUp"; LL.bindClass("android.app.AlertDialog"); LL.bindClass("android.content.DialogInterface"); LL.bindClass("android.widget.Toast"); LL.bindClass("android.content.pm.PackageManager"); LL.bindClass('java.lang.Runtime'); LL.bindClass('java.io.DataInputStream'); LL.bindClass('java.io.DataOutputStream'); LL.bindClass('java.lang.StringBuffer'); LL.bindClass("java.lang.Runnable"); LL.bindClass("java.lang.Thread"); LL.bindClass("android.os.Handler"); LL.bindClass("android.os.Looper"); var e = LL.getEvent() , it = e.getItem() , script = LL.getCurrentScript() , context = LL.getContext() , data = e.getData() , frozenItIdss = [] , threads = [] , GUIHandler = new Handler() , frozenApps; function loanEventHandler(ob, name, action, data){ var prop = ob.getProperties() , ed = prop.edit() , evHa = prop.getEventHandler(name); evHa = [evHa.getAction(), evHa.getData()]; ed.setEventHandler(name, action, data); ed.commit(); ob.setTag("old "+name+" evHa", JSON.stringify(evHa)); } function returnEventHandler(ob, name){ var ed = ob.getProperties().edit() , evHa = JSON.parse(ob.getTag("old "+name+" evHa")); ed.setEventHandler(name, evHa[0], evHa[1]); ed.commit(); ob.setTag("name", null); } function customConfirmDialog(title,onConfirmFunction){ var builder=new AlertDialog.Builder(context); builder.setCancelable(true); builder.setTitle(title); builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener(){onClick:function(dialog,id){dialog.dismiss();}}); builder.setPositiveButton("Confirm",new DialogInterface.OnClickListener(){onClick:function(dialog,id){dialog.dismiss();setTimeout(onConfirmFunction,0);}}); builder.show(); } function uninstall(c){ if(c){ c.setTag("longTapFreeze",false); returnEventHandler(c, freezeEvent); returnEventHandler(c, menuEvent); if(c.getTag("autosync")=="true")returnEventHandler(c, "resumed"); var frozenItIds = getFrozenItIds(c) , it; frozenItIds.forEach(function(itId, ind){ if(it = c.getItemById(itId)){ unfreezeEffect(it); } }) frozenItIds.clear(); frozenItIds.queueSave(); } } function runAsRoot(cmd, newThread, callback) { function execCmd(cmd){ try { var process = Runtime.getRuntime().exec("su") , reader = new DataInputStream(process.getInputStream()) , writer = new DataOutputStream(process.getOutputStream()); writer.writeBytes(cmd + '\n'); writer.flush(); writer.close(); var tmp , output = []; while ((tmp = reader.readLine()) != null) { output.push(tmp); } reader.close(); process.waitFor(); return output; } catch (err) { alert(err+"At line "+err.lineNumber); } } if(newThread==false){ return execCmd(cmd); }else{ var handler = getHandler(); startNewBackgroundThread(function(){ if(callback){ var output = execCmd(cmd); handler.post(function(){ callback(output); }); }else{ execCmd(cmd) } }) } } function getPackageName(it){ try{ return it.getIntent().getComponent().getPackageName(); }catch(e){ Toast.makeText(context, "This item doesn't launch an app!", Toast.LENGTH_SHORT).show(); return null; } } function isFreezable(pkgName){ if(!pkgName) return false; var onBlackList = false; for(var i=0;i<blackList.length;i++){ if(pkgName==blackList[i]){ onBlackList=true; break; } } if(whiteList.length==0){ onWhiteList = true; }else{ onWhiteList = false; for(var i=0;i<whiteList.length;i++){ if(pkgName==whiteList[i]){ onWhiteList=true; break; } } } return !onBlackList && onWhiteList; } function freeze(it, pkgName){ runAsRoot("pm disable "+pkgName, true, function(){ freezeEffect(it); var frozenItIds = getFrozenItIds(it.getParent()); frozenItIds.push(it.getId()); frozenItIds.queueSave(); }); } function unfreeze(it, pkgName){ runAsRoot("pm enable "+pkgName, true, function(){ unfreezeEffect(it); var frozenItIds = getFrozenItIds(it.getParent()); frozenItIds.remove(it.getId()); frozenItIds.queueSave(); }); } function freezeEffect(it){ it.getProperties().edit().setInteger("s.iconColorFilter",0x00ffffff).commit(); it.setTag("frozen",true); } function unfreezeEffect(it){ it.getProperties().edit().setInteger("s.iconColorFilter",0xffffffff).commit(); it.setTag("frozen",false); } function getFrozenItIds(c){ var cId = c.getId(); if(!frozenItIdss[cId]) frozenItIdss[cId] = new TagArray(c, "frozenItIds"); return frozenItIdss[cId]; } function getFrozenApps(){ if(!frozenApps){ frozenApps = runAsRoot("pm list packages -d", false); for(var i=0;i<frozenApps.length;i++){ frozenApps[i] = frozenApps[i].split(":")[1]; } } return frozenApps; } function syncContainer(c){ startNewBackgroundThread(function(){ var items = c.getItems(); for(var i = 0; i<items.length; i++){ syncItem(items.getAt(i)); } }); } function syncItem(it){ var c = it.getParent() , frozenItIds = getFrozenItIds(c) , frozenApps = getFrozenApps(); if(it.getType()=="Shortcut"){ var pkgName = getPackageName(it); if(pkgName && isFreezable(pkgName)){ var matched = false , isFrozen = it.getTag("frozen")=="true"; for(var j = 0; j<frozenApps.length; j++){ if(pkgName==frozenApps[j]){ matched = true; if(isFreezable(it) && !isFrozen){ handleGUIEdit(function(){ freezeEffect(it); }) frozenItIds.push(it.getId()); frozenItIds.queueSave(); } } } if(!matched && isFrozen){ handleGUIEdit(function(){ unfreezeEffect(it); }) frozenItIds.remove(it.getId()); frozenItIds.queueSave(); } } } } function handleGUIEdit(func){ if (Looper.getMainLooper().getThread() == Thread.currentThread()) { func(); } else { GUIHandler.post(func); } } function startNewBackgroundThread(func){ var thread = new Thread(function(){ func(); if(threads[Thread.currentThread().getId()].prepared==true){ Looper.loop(); } }) thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler(){ uncaughtException: function(th, ex){ handleGUIEdit(function(){ alert(ex.getMessage(), logScript.logLevel.ERROR); }) } }) threads[thread.getId()] = {}; thread.start(); } function getHandler(){ if (Looper.getMainLooper().getThread() == Thread.currentThread()) { return GUIHandler; }else{ var threadId = Thread.currentThread().getId(); if(threads[threadId].prepared!=true){ Looper.prepare(); threads[threadId].prepared = true; } return new Handler(); } } function TagArray(ob, name, manualSave){ this.push.apply(this, JSON.parse(ob.getTag(name)) || []); var me = this; this.save = function(){ ob.setTag(name, JSON.stringify(Array.prototype.slice.call(me))); } if(manualSave!=true) this.queueSave(); } TagArray.prototype = new Array(); TagArray.prototype.constructor = TagArray; TagArray.prototype.saveQueued = false; TagArray.prototype.queueSave = function(){ if(!this.queueSaved){ getHandler().post(this.save) this.queueSaved = true; } } TagArray.prototype.remove = function(el){ var ind = this.indexOf(el); if(ind != -1) this.splice(ind, 1); else return false; } TagArray.prototype.clear = function(){ this.length = 0; } if(data){ if(data.length == 4){ var c; if(it){ syncContainer(it.getParent()); }else if(c = e.getContainer()){ syncContainer(c); }else{ throw new Error("Could not find a way to determine the container to sync. Run the script from an item in the container or from the container itself or add the container id directly afrer 'sync'.") } }else if(data.substring(0,4) == "sync"){ var cId = data.substring(4, data.length) , c = LL.getContainerById(cId); if(!c)throw new Error("Could not find container with id:"+ cId) syncContainer(c); } }else{ if(!it){ var c = e.getContainer() , cIds = new TagArray(script, "cIds", true); if(c){ cId = c.getId(); if(c.getTag("longTapFreeze")=="true"){ customConfirmDialog("Are you sure you want to uninstall?", function(){ uninstall(c); cIds.remove(c.getId()); cIds.save(); Toast.makeText(context, "Uninstalled!", Toast.LENGTH_SHORT).show(); }); }else{ customConfirmDialog("Are you sure you want to install?", function(){ cIds.push(c.getId()); c.setTag("longTapFreeze",true); loanEventHandler(c, freezeEvent, EventHandler.RUN_SCRIPT, script.getId()); loanEventHandler(c, menuEvent, EventHandler.RUN_SCRIPT, script.getId()); customConfirmDialog("Do you want to enable autosync?", function(){ loanEventHandler(c, "resumed", EventHandler.RUN_SCRIPT, script.getId()+"sync"); c.setTag("autosync", true); }); cIds.save(); Toast.makeText(context, "Installed!", Toast.LENGTH_SHORT).show(); }); } }else{ customConfirmDialog("Are you sure you want to uninstall from every container?", function(){ for(var i=0;i<cIds.length;i++){ uninstall(LL.getContainerById(cIds[i])); } cIds.clear(); cIds.save(); Toast.makeText(context, "Uninstalled everywhere!", Toast.LENGTH_SHORT).show(); }); } } else { var src = e.getSource(); if(src==freezeSource){ var c = it.getParent() , pkgName = getPackageName(it); if(isFreezable(pkgName)){ if(it.getTag("frozen")=="true"){ unfreeze(it, pkgName); }else{ freeze(it, pkgName); } }else{ Toast.makeText(context, "Cannot freeze/unfreeze! (Probably because of black- or whitelist", Toast.LENGTH_SHORT).show(); } }else if(src == menuSource){ LL.bindClass("android.R"); LL.bindClass("java.util.ArrayList"); LL.bindClass("android.view.ViewTreeObserver"); LL.bindClass("android.widget.Button"); LL.bindClass("android.view.View"); LL.bindClass("android.os.Build"); var pkg = context.getPackageName() , rsrc = context.getResources() , id = rsrc.getIdentifier("bubble_content", "id", pkg) , menu = context.getWindow().getDecorView().findViewById(id) , menuRoot = menu.getParent() , version = Build.VERSION.SDK_INT; function add(text,onClickFunction,first,list){ var t=new Button(LL.getContext()); if(version >= 16) t.setBackground(first.getBackground().mutate().getConstantState().newDrawable()); else t.setBackgroundDrawable(first.getBackground().mutate().newDrawable()); t.setTypeface(first.getTypeface()); if(version >= 14) t.setAllCaps(false); t.setTextSize(0,first.getTextSize()); if(version >= 21) t.setFontFeatureSettings(first.getFontFeatureSettings()); t.setGravity(first.getGravity()); t.setText(text); t.setOnClickListener(new View.OnClickListener(){ onClick:onClickFunction }); list.addView(t); } var obs=menuRoot.getViewTreeObserver() , l=new ViewTreeObserver.OnGlobalLayoutListener(){ onGlobalLayout:function(){ var list=menu , first=list.getChildAt(0); add("Sync", function(){ syncItem(it); },first, list) obs.removeOnGlobalLayoutListener(l); return true; } }; obs.addOnGlobalLayoutListener(l); LL.runAction(EventHandler.LAUNCHER_MENU); } } } </sxh>

script_autosetupappfreeze.1472470113.txt.gz · Last modified: 2016/08/29 11:28 by cdfa