User Tools

Site Tools


script_calendar_widget

====== About the script ====== * Purpose : This script will allow you to set up a calendar widget replacement * Author : F43nd1r * Current Version : 1.3 * Link : https://plus.google.com/+LukasMorawietz/posts/ENEoGArx8D4 ====== Changelog ====== * Version 1.0 (19/4/2015): initial release in wiki * Version 1.1 (20/4/2015): fixed showing yesterday's all-day-appointments, reduced minimum API level to 14 * Version 1.2 (21/4/2015): added fix for devices using 23:59 instead of 00:00 (manual) * Version 1.3 (21/4/2015): Fix memory leaks ====== How to use the script ==== * Either install Calendar package from [[http://www.pierrox.net/android/applications/lightning_launcher/permissions/|here]] or grant READ_ CALENDAR permission using Lightning Permission Manager * Create a text item * set this script in its resumed event * set up your configuration at the beginning of the script * set the Max lines of the item to showMax or higher * customize the items style to your needs ====== Issues and hints ====== * //please report all bugs in the g+ community// ====== Script code ====== <sxh javascript;>//config var pattern = "dd. MM. HH:mm";//format has to match http://developer.android.com/reference/java/text/SimpleDateFormat.html var showEnd = true; var patternEnd = "HH:mm"; //used when entry ends on same day var patternAllDay = "dd. MM.";//used for all-day-entries var showMax = 3; var days = 90; //how far to search from today (might be not exact) var dayOffFix=false; //when true adds one to all-day entries //endconfig LL.bindClass("android.provider.CalendarContract"); LL.bindClass("java.util.Calendar"); LL.bindClass("android.content.ContentUris"); LL.bindClass("java.text.SimpleDateFormat"); LL.bindClass("android.app.AlertDialog"); LL.bindClass("android.widget.SimpleCursorAdapter"); LL.bindClass("android.R"); LL.bindClass("android.content.DialogInterface"); LL.bindClass("android.widget.AbsListView"); var Calendars = CalendarContract.Calendars; var Instances = CalendarContract.Instances; var item=LL.getEvent().getItem(); var saved = item.getTag("calendar"); var cx = LL.getContext(); var cr = cx.getContentResolver() if(saved==null){ var projection = [Calendars._ID,Calendars.NAME]; var calCursor = cr.query(Calendars.CONTENT_URI, projection, Calendars.VISIBLE + " = 1", null, Calendars._ID + " ASC"); if (!calCursor.moveToFirst()){ AndroiSD. makeNewToast("No Calendar found").show(); calCursor.close(); return; } var adapter = new SimpleCursorAdapter(cx,R.layout.simple_list_item_multiple_choice,calCursor,[Calendars.NAME],[R.id.text1],0); var dialog = new AlertDialog.Builder(cx) .setAdapter(adapter,null) .setPositiveButton("Confirm", new DialogInterface.OnClickListener(){ onClick:function(dialog,which){ var checked = dialog.getListView().getCheckedItemPositions(); var cals = []; for(var i=0;i<checked.size();i++){ calCursor.moveToPosition(i); if(checked.get(checked.keyAt(i))) cals.push(calCursor.getLong(0)); } item.setTag("calendar",JSON.stringify(cals)); update(cals); Android.makeNewToast("Saved!",true).show(); calCursor.close(); } }) .setNegativeButton("Cancel",null) .setTitle("Select Calendar(s)") .create(); dialog.getListView().setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE); dialog.show(); } else update(JSON.parse(saved)); function update(cals){ if(cals.length<1)return; var cal = Calendar.getInstance(); var begin = cal.getTimeInMillis(); cal.add(Calendar.DAY_OF_YEAR,days); var end = cal.getTimeInMillis(); var proj = [ Instances._ID, Instances.BEGIN, Instances.END,Instances.TITLE,Instances.ALL_DAY]; var uriBuilder = Instances.CONTENT_URI.buildUpon(); ContentUris.appendId(uriBuilder,begin); ContentUris.appendId(uriBuilder,end); var s="('"; for(var i=0;i<cals.length-1;i++){ s+=cals[i]+"','"; } s+=cals[cals.length-1]+"')"; var cursor = LL.getContext().getContentResolver().query(uriBuilder.build(), proj, Instances.CALENDAR_ID+" IN "+s,null,Instances.BEGIN+" ASC"); if(!cursor.moveToFirst()){ cursor.close(); return; } var s=""; for(var i=0;i<showMax;i++) { var c=Calendar.getInstance(); c.setTimeInMillis(cursor.getLong(1)); var c2=c.clone(); c2.setTimeInMillis(cursor.getLong(2)); var format=new SimpleDateFormat(pattern); var formatEnd=new SimpleDateFormat(patternEnd); var formatAllDay=new SimpleDateFormat(patternAllDay); var isAllDay=(cursor.getInt(4)==1); s+= cursor.getString(3); if(isAllDay)(dayOffFix?c:c2).add(Calendar.DAY_OF_YEAR,dayOffFix?1:-1); var endsOnSame=(c.get(Calendar.DAY_OF_YEAR)==c2.get(Calendar.DAY_OF_YEAR)); if(!isAllDay){ s+=" "+format.format(c.getTime()); if(showEnd){ if(endsOnSame) s+=" - "+formatEnd.format(c2.getTime()); else s+=" - "+format.format(c2.getTime()); } } else{ s+=" "+formatAllDay.format(c.getTime()); if(!endsOnSame&&showEnd)s+=" - "+formatAllDay.format(c2.getTime()); } if(cursor.moveToNext())s+="\n"; else break; } LL.getEvent().getItem().setLabel(s, true); cursor.close(); } </sxh>

script_calendar_widget.txt · Last modified: 2016/06/28 12:11 by f43nd1r