Skip to content

Commit 94a1ceb

Browse files
committed
add event to calendar
1 parent a3c9ecd commit 94a1ceb

File tree

2 files changed

+151
-4
lines changed

2 files changed

+151
-4
lines changed

src/com/fami/event/EventDetailActivity.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,56 @@ public void onClick(DialogInterface dialog, int which){
164164
QBCustomObjects.createObject(event, new QBEntityCallbackImpl<QBCustomObject>() {
165165
@Override
166166
public void onSuccess(QBCustomObject createdObject, Bundle bundle) {
167+
String calId = "";
168+
Cursor userCursor = getContentResolver().query(Uri.parse(calanderURL), null,
169+
null, null, null);
170+
if(userCursor.getCount() > 0){
171+
userCursor.moveToFirst();
172+
calId = userCursor.getString(userCursor.getColumnIndex("_id"));
173+
174+
}
175+
ContentValues events = new ContentValues();
176+
events.put("title", event_name);
177+
events.put("description", event_description);
178+
events.put("calendar_id",calId);
179+
180+
Calendar mCalendar = Calendar.getInstance();
181+
mCalendar.set(Task_year,Task_month - 1,Task_day);
182+
mCalendar.set(Calendar.HOUR_OF_DAY,start_hours);
183+
mCalendar.set(Calendar.MINUTE,start_min);
184+
long start = mCalendar.getTime().getTime();
185+
mCalendar.set(Calendar.HOUR_OF_DAY,end_hours);
186+
mCalendar.set(Calendar.MINUTE,end_min);
187+
long end = mCalendar.getTime().getTime();
188+
189+
events.put("dtstart", start);
190+
events.put("dtend", end);
191+
events.put("hasAlarm",1);
192+
events.put("eventTimezone", Time.getCurrentTimezone());
193+
194+
if (option.equals("Every Year")){
195+
events.put("rrule", "FREQ=YEARLY");
196+
}
197+
else if (option.equals("Every Month")){
198+
events.put("rrule", "FREQ=MONTHLY");
199+
}
200+
else if (option.equals("Every Week")){
201+
events.put("rrule", "FREQ=WEEKLY");
202+
}
203+
else if (option.equals("Every day")){
204+
events.put("rrule", "FREQ=DAILY");
205+
}
206+
else if (option.equals("None")){
207+
}
208+
209+
Uri newEvent = getContentResolver().insert(Uri.parse(calanderEventURL), events);
210+
long id = Long.parseLong( newEvent.getLastPathSegment() );
211+
ContentValues values1 = new ContentValues();
212+
values1.put( "event_id", id );
213+
values1.put( "method", 1 );
214+
values1.put( "minutes", 10 );
215+
getContentResolver().insert(Uri.parse(calanderRemiderURL), values1);
216+
Toast.makeText(EventDetailActivity.this, "Successful", Toast.LENGTH_LONG).show();
167217
backToEventList();
168218
}
169219

src/com/fami/event/EventListActivity.java

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import android.net.Uri;
1414
import android.os.Bundle;
1515
import android.support.v4.app.FragmentActivity;
16+
import android.text.format.Time;
1617
import android.util.Log;
1718
import android.view.View;
1819
import android.widget.*;
@@ -27,11 +28,13 @@
2728
import com.quickblox.customobjects.QBCustomObjects;
2829
import com.quickblox.customobjects.model.QBCustomObject;
2930

30-
public class EventListActivity extends FragmentActivity {
31+
public class EventListActivity<SettingActivity> extends FragmentActivity {
3132
private ListView event_list;
3233
private EventAdapter eventAdapter;
3334
private static String calanderURL = "content://com.android.calendar/calendars";
34-
35+
private static String calanderEventURL = "content://com.android.calendar/events";
36+
private static String calanderRemiderURL = "content://com.android.calendar/reminders";
37+
3538
@Override
3639
public void onCreate(Bundle savedInstanceState) {
3740
super.onCreate(savedInstanceState);
@@ -58,11 +61,105 @@ public void onError(List<String> errors) {
5861
public void onAddtoCalender(View view) {
5962
View v = (View) view.getParent();
6063
TextView EventName = (TextView) v.findViewById(R.id.event_name);
61-
String event_name = EventName.getText().toString();
64+
final String event_name = EventName.getText().toString();
6265
TextView EventDate = (TextView) v.findViewById(R.id.event_date);
6366
String event_date = EventDate.getText().toString();
6467
TextView EventRepeat = (TextView) v.findViewById(R.id.event_repeat);
65-
String event_repeat = EventRepeat.getText().toString();
68+
final String event_repeat = EventRepeat.getText().toString();
69+
String[] num = {"","",""};
70+
71+
int j = 0;
72+
int n = 0;
73+
for (j = 0; j < event_date.length(); j++){
74+
if (event_date.charAt(j) != '/'){
75+
num[0] = num[0] + event_date.charAt(j);
76+
}else{
77+
break;
78+
}
79+
}
80+
n = j + 1;
81+
for (j = n; j < event_date.length(); j++){
82+
if (event_date.charAt(j) != '/'){
83+
num[1] = num[1] + event_date.charAt(j);
84+
}else{
85+
break;
86+
}
87+
}
88+
n = j + 1;
89+
for (j = n; j < event_date.length(); j++){
90+
if (event_date.charAt(j) != '/'){
91+
num[2] = num[2] + event_date.charAt(j);
92+
}else{
93+
break;
94+
}
95+
}
96+
97+
final int Task_year = Integer.parseInt(num[0]);
98+
final int Task_month = Integer.parseInt(num[1]);
99+
final int Task_day = Integer.parseInt(num[2]);
100+
101+
AlertDialog.Builder builder = new AlertDialog.Builder(this);
102+
builder.setTitle("Confirm");
103+
builder.setMessage("Do you want to add to google calendar?");
104+
105+
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
106+
@Override
107+
public void onClick(DialogInterface dialogInterface, int i) {
108+
109+
String calId = "";
110+
Cursor userCursor = getContentResolver().query(Uri.parse(calanderURL), null,
111+
null, null, null);
112+
if(userCursor.getCount() > 0){
113+
userCursor.moveToFirst();
114+
calId = userCursor.getString(userCursor.getColumnIndex("_id"));
115+
116+
}
117+
ContentValues event = new ContentValues();
118+
event.put("title", event_name);
119+
event.put("description", event_name);
120+
event.put("calendar_id",calId);
121+
if (event_repeat.equals("356")){
122+
event.put("rrule", "FREQ=YEARLY");
123+
}
124+
else if (event_repeat.equals("31")){
125+
event.put("rrule", "FREQ=MONTHLY");
126+
}
127+
else if (event_repeat.equals("7")){
128+
event.put("rrule", "FREQ=WEEKLY");
129+
}
130+
else if (event_repeat.equals("1")){
131+
event.put("rrule", "FREQ=DAILY");
132+
}
133+
else if (event_repeat.equals("0")){
134+
}
135+
136+
137+
138+
Calendar mCalendar = Calendar.getInstance();
139+
mCalendar.set(Task_year,Task_month - 1,Task_day);
140+
mCalendar.set(Calendar.HOUR_OF_DAY,12);
141+
long start = mCalendar.getTime().getTime();
142+
mCalendar.set(Calendar.HOUR_OF_DAY,23);
143+
long end = mCalendar.getTime().getTime();
144+
145+
event.put("dtstart", start);
146+
event.put("dtend", end);
147+
event.put("hasAlarm",1);
148+
event.put("eventTimezone", Time.getCurrentTimezone());
149+
150+
Uri newEvent = getContentResolver().insert(Uri.parse(calanderEventURL), event);
151+
long id = Long.parseLong( newEvent.getLastPathSegment() );
152+
ContentValues values1 = new ContentValues();
153+
values1.put( "event_id", id );
154+
values1.put( "method", 1 );
155+
values1.put( "minutes", 10 );
156+
getContentResolver().insert(Uri.parse(calanderRemiderURL), values1);
157+
Toast.makeText(EventListActivity.this, "Successful", Toast.LENGTH_LONG).show();
158+
159+
}
160+
});
161+
162+
builder.setNegativeButton("No",null);
66163

67164
}
68165
protected void setEventAdapter(ArrayList<QBCustomObject> customObjects) {

0 commit comments

Comments
 (0)