New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding support for Calendar events #310
Changes from 7 commits
587943c
7c8226f
c52a5bb
cc3307f
39aea75
c0b39bf
18ff18b
e9d13ba
62cd080
2b56beb
d05ea77
6d35e27
86817fa
93da2ee
d26746c
0eb18f8
2590936
88d72d2
51289de
a2c9ec9
550c201
c912dd9
a6185c1
af847d9
7f22a1f
482ca31
1c2d51a
f3e8de1
8dda044
37f857c
9e84cae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package fr.neamar.kiss.dataprovider; | ||
|
||
import android.content.Context; | ||
|
||
import java.util.ArrayList; | ||
import java.util.regex.Pattern; | ||
|
||
import fr.neamar.kiss.loader.LoadAppPojos; | ||
import fr.neamar.kiss.loader.LoadEventPojos; | ||
import fr.neamar.kiss.normalizer.StringNormalizer; | ||
import fr.neamar.kiss.pojo.AliasPojo; | ||
import fr.neamar.kiss.pojo.AppPojo; | ||
import fr.neamar.kiss.pojo.EventPojo; | ||
import fr.neamar.kiss.pojo.Pojo; | ||
import fr.neamar.kiss.pojo.SettingPojo; | ||
|
||
public class EventProvider extends Provider<EventPojo> { | ||
private final String eventsName; | ||
|
||
public EventProvider(Context context) { | ||
super(new LoadEventPojos(context)); | ||
eventsName="Events: ".toLowerCase(); | ||
} | ||
|
||
public ArrayList<Pojo> getResults(String query) { | ||
query = StringNormalizer.normalize(query); | ||
ArrayList<Pojo> results = new ArrayList<>(); | ||
|
||
int relevance; | ||
String eventNameLowerCased; | ||
for (EventPojo event : pojos) { | ||
relevance = 0; | ||
eventNameLowerCased = event.nameNormalized; | ||
if (eventNameLowerCased.startsWith(query)) { | ||
relevance = 10; | ||
} | ||
else if (eventNameLowerCased.contains(query)) { | ||
relevance = 5; | ||
} | ||
else if (eventsName.startsWith(query)) { | ||
// Also display for a search on "events" for instance | ||
relevance = 4; | ||
} | ||
|
||
if (relevance>0) | ||
{ | ||
event.displayName = event.name.replaceFirst( | ||
"(?i)(" + Pattern.quote(query) + ")", "{$1}"); | ||
|
||
//event.setDisplayNameHighlightRegion(matchPositionStart, matchPositionEnd); | ||
event.relevance = relevance; | ||
results.add(event); | ||
} | ||
} | ||
|
||
return results; | ||
} | ||
|
||
/** | ||
* Return a Pojo | ||
* | ||
* @param id we're looking for | ||
* @param allowSideEffect do we allow this function to have potential side effect? Set to false to ensure none. | ||
* @return an apppojo, or null | ||
*/ | ||
public Pojo findById(String id, Boolean allowSideEffect) { | ||
for (Pojo pojo : pojos) { | ||
if (pojo.id.equals(id)) { | ||
// Reset displayName to default value | ||
if (allowSideEffect) { | ||
pojo.displayName = pojo.name; | ||
} | ||
return pojo; | ||
} | ||
|
||
} | ||
|
||
return null; | ||
} | ||
|
||
public Pojo findById(String id) { | ||
return findById(id, true); | ||
} | ||
|
||
public ArrayList<Pojo> getAllEvents() { | ||
ArrayList<Pojo> records = new ArrayList<>(pojos.size()); | ||
records.trimToSize(); | ||
|
||
for (Pojo pojo : pojos) { | ||
pojo.displayName = pojo.name; | ||
records.add(pojo); | ||
} | ||
return records; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package fr.neamar.kiss.loader; | ||
|
||
import android.content.Context; | ||
import android.database.Cursor; | ||
import android.net.Uri; | ||
import android.provider.CalendarContract; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
|
||
import fr.neamar.kiss.normalizer.StringNormalizer; | ||
import fr.neamar.kiss.pojo.EventPojo; | ||
|
||
/** | ||
* Created by nmitsou on 09.11.15. | ||
*/ | ||
public class LoadEventPojos extends LoadPojos<EventPojo> { | ||
public LoadEventPojos(Context context) { | ||
super(context, "none://"); | ||
} | ||
|
||
@Override | ||
protected ArrayList<EventPojo> doInBackground(Void... params) { | ||
|
||
ArrayList<EventPojo> events = new ArrayList<>(); | ||
|
||
String[] proj = | ||
new String[]{ | ||
CalendarContract.Events._ID, | ||
CalendarContract.Events.DTSTART, | ||
CalendarContract.Events.DTEND, | ||
CalendarContract.Events.TITLE, | ||
CalendarContract.Events.DESCRIPTION}; | ||
|
||
|
||
String selectionClause = "(dtstart >= ? and dtend <=?)"; | ||
String[] selectionsArgs = new String[]{"" + new Date().getTime(), ""+ getDateInFuture(new Date()).getTime()}; | ||
|
||
Cursor cursor = context.getContentResolver() | ||
.query( | ||
Uri.parse("content://com.android.calendar/events"), | ||
proj, selectionClause, | ||
selectionsArgs, "dtstart"); | ||
|
||
|
||
cursor.moveToFirst(); | ||
// fetching calendars name | ||
int eventsCount = cursor.getCount(); | ||
|
||
for (int i = 0; i < eventsCount; i++) { | ||
|
||
EventPojo event = new EventPojo(); | ||
event.description = cursor.getString(4); | ||
event.title = cursor.getString(3); | ||
event.id = cursor.getString(0); | ||
event.startDate = getDate(Long.parseLong(cursor.getString(1))); | ||
event.stopDate = getDate(Long.parseLong(cursor.getString(2))); | ||
event.setName(event.startDate+" "+event.title); | ||
event.nameNormalized = StringNormalizer.normalize(event.name); | ||
events.add(event); | ||
cursor.moveToNext(); | ||
|
||
} | ||
return events; | ||
|
||
} | ||
public static String getDate(long milliSeconds) { | ||
SimpleDateFormat formatter = new SimpleDateFormat( | ||
"dd/MM/yy hh:mm a"); | ||
Calendar calendar = Calendar.getInstance(); | ||
calendar.setTimeInMillis(milliSeconds); | ||
return formatter.format(calendar.getTime()); | ||
} | ||
|
||
//method created for demonstration purposes | ||
public Date getDateInFuture(Date date) { | ||
Calendar calendar = Calendar.getInstance(); | ||
calendar.setTime(date); | ||
calendar.add(Calendar.DATE, 30); //1 month | ||
return calendar.getTime(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package fr.neamar.kiss.pojo; | ||
|
||
/** | ||
* Created by nmitsou on 09.11.15. | ||
*/ | ||
public class EventPojo extends Pojo { | ||
|
||
public String id; | ||
public String startDate; | ||
public String stopDate; | ||
public String title; | ||
public String description; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package fr.neamar.kiss.result; | ||
|
||
import android.annotation.TargetApi; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.graphics.drawable.Drawable; | ||
import android.net.Uri; | ||
import android.os.Build; | ||
import android.provider.CalendarContract; | ||
import android.provider.ContactsContract; | ||
import android.view.MenuItem; | ||
import android.view.View; | ||
import android.widget.PopupMenu; | ||
import android.widget.TextView; | ||
|
||
import fr.neamar.kiss.R; | ||
import fr.neamar.kiss.adapter.RecordAdapter; | ||
import fr.neamar.kiss.pojo.EventPojo; | ||
import fr.neamar.kiss.pojo.PhonePojo; | ||
|
||
/** | ||
* Created by nmitsou on 09.11.15. | ||
*/ | ||
public class EventResult extends Result { | ||
private final EventPojo eventPojo; | ||
|
||
public EventResult(EventPojo eventPojo) { | ||
super(); | ||
this.pojo = this.eventPojo = eventPojo; | ||
} | ||
|
||
@Override | ||
public View display(Context context, int position, View v) { | ||
if (v == null) | ||
v = inflateFromId(context, R.layout.item_event); | ||
|
||
TextView appName = (TextView) v.findViewById(R.id.item_event_text); | ||
String text = context.getString(R.string.ui_item_event); | ||
//appName.setText(eventPojo.startDate + " " + eventPojo.title); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here aswell |
||
appName.setText(enrichText(eventPojo.displayName)); | ||
|
||
return v; | ||
} | ||
|
||
|
||
|
||
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) | ||
@Override | ||
public void doLaunch(Context context, View v) { | ||
Intent calendar = new Intent(Intent.ACTION_VIEW); | ||
Uri.Builder uri = CalendarContract.Events.CONTENT_URI.buildUpon(); | ||
uri.appendPath(eventPojo.id); | ||
calendar.setData(uri.build()); | ||
context.startActivity(calendar); | ||
} | ||
|
||
@Override | ||
protected Boolean popupMenuClickHandler(Context context, RecordAdapter parent, MenuItem item) { | ||
/*switch (item.getItemId()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here too |
||
case R.id.item_phone_createcontact: | ||
// Create a new contact with this phone number | ||
Intent createIntent = new Intent(Intent.ACTION_INSERT); | ||
createIntent.setType(ContactsContract.Contacts.CONTENT_TYPE); | ||
createIntent.putExtra(ContactsContract.Intents.Insert.PHONE, phonePojo.phone); | ||
createIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | ||
context.startActivity(createIntent); | ||
return true; | ||
case R.id.item_phone_sendmessage: | ||
String url = "sms:" + phonePojo.phone; | ||
Intent messageIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse(url)); | ||
messageIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | ||
context.startActivity(messageIntent); | ||
return true; | ||
}*/ | ||
|
||
return super.popupMenuClickHandler(context, parent, item); | ||
} | ||
|
||
|
||
@Override | ||
public Drawable getDrawable(Context context) { | ||
return context.getResources().getDrawable(android.R.drawable.ic_menu_call); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:gravity="center_vertical" | ||
android:orientation="horizontal" | ||
android:paddingBottom="@dimen/result_margin_bottom" | ||
android:paddingLeft="@dimen/result_margin_left" | ||
android:paddingRight="@dimen/result_margin_right" | ||
android:paddingTop="@dimen/result_margin_top"> | ||
|
||
<ImageView | ||
android:id="@+id/item_event_icon" | ||
android:layout_width="48dp" | ||
android:layout_height="48dp" | ||
android:layout_marginBottom="@dimen/icon_margin_bottom" | ||
android:layout_marginLeft="@dimen/icon_margin_left" | ||
android:layout_marginRight="@dimen/icon_margin_right" | ||
android:layout_marginTop="@dimen/icon_margin_top" | ||
android:contentDescription="@null" | ||
android:src="@drawable/ic_event"/> | ||
|
||
<TextView | ||
android:id="@+id/item_event_text" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/ui_item_event" | ||
android:textColor="?attr/resultColor" | ||
android:textSize="@dimen/result_title_size"/> | ||
|
||
</LinearLayout> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
<string name="activity_setting">KISS settings</string> | ||
<string name="ui_search_hint">Search apps, contacts, …</string> | ||
<string name="ui_item_search">Search for “%s”</string> | ||
<string name="ui_item_event">Event: “%s”</string> | ||
<string name="ui_item_phone">Call “%s”</string> | ||
<string name="ui_item_contact_hint_message">Message</string> | ||
<string name="ui_item_contact_hint_call">Call</string> | ||
|
@@ -30,6 +31,9 @@ | |
<string name="aliases_desc">Enable aliases (convenience shortcuts)</string> | ||
<string name="aliases_name">Aliases</string> | ||
|
||
<string name="events_desc">Enable calendar events</string> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe "Search in my calendar events" is better? (i.e. same as "Search in my contacts") |
||
<string name="events_name">Calendar</string> | ||
|
||
<string name="contacts_desc">Search in my contacts</string> | ||
<string name="contacts_name">Contacts</string> | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commented code