Android Open Source - android-calendar-api-sample Calendar Intent Fragment






From Project

Back to project page android-calendar-api-sample.

License

The source code is released under:

MIT License

If you think the Android project android-calendar-api-sample listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.roottony.calendar;
/*ww w  . j  a  va 2s.  co  m*/
import android.annotation.TargetApi;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.Calendar;

public class CalendarIntentFragment extends Fragment {

    private static final int TITLE_RESOURCE = R.string.title_intents_demo;

    public static CalendarIntentFragment newInstance() {
        return new CalendarIntentFragment();
    }

    public CalendarIntentFragment() {
    }

    public static int getTitleResource() {
        return TITLE_RESOURCE;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_calendar_intents, container, false);

        if (rootView != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                addListener(rootView.findViewById(R.id.btn_add_event), new OnAddEventListener());
                addListener(rootView.findViewById(R.id.btn_edit_event), new OnEditEventListener());
                addListener(rootView.findViewById(R.id.btn_view_event), new OnViewEventListener());
                addListener(rootView.findViewById(R.id.btn_view_time), new OnViewTimeListener());
            } else {
                rootView.findViewById(R.id.layout_intent_buttons).setVisibility(View.GONE);
                rootView.findViewById(R.id.tv_intents_not_supported).setVisibility(View.VISIBLE);
            }
        }
        return rootView;
    }

    private void addListener(View button, View.OnClickListener listener) {
        button.setOnClickListener(listener);
    }

    private class OnAddEventListener implements View.OnClickListener {

        @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        @Override
        public void onClick(View v) {
            Context context = v.getContext();

            Calendar beginTime = Calendar.getInstance();
            beginTime.set(2014, Calendar.APRIL, 1, 6, 0);   // Wake up and enjoy the 1st of April!
            Calendar endTime = Calendar.getInstance();
            endTime.set(2014, Calendar.AUGUST, 1, 6, 0);
            Intent intent = new Intent(Intent.ACTION_INSERT)
                    .setData(CalendarContract.Events.CONTENT_URI)
                    .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis())
                    .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis())
                    .putExtra(CalendarContract.Events.TITLE, context.getString(R.string.first_april_event_title))
                    .putExtra(CalendarContract.Events.DESCRIPTION,
                              context.getString(R.string.first_april_event_description))
                    .putExtra(CalendarContract.Events.EVENT_LOCATION, context.getString(R.string.bed))
                    .putExtra(CalendarContract.Events.AVAILABILITY, CalendarContract.Events.AVAILABILITY_BUSY)
                    .putExtra(Intent.EXTRA_EMAIL, context.getString(R.string.first_april_event_attenders));
            startActivity(intent);
        }
    }

    private class OnEditEventListener implements View.OnClickListener {

        @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        @Override
        public void onClick(View v) {
            Context context = v.getContext();

            long eventID = 437;  // enter valid event id here
            Uri uri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventID);
            Intent intent = new Intent(Intent.ACTION_EDIT)
                        .setData(uri)
                        .putExtra(CalendarContract.Events.TITLE,
                                  context.getString(R.string.first_april_event_title_renamed));
            startActivity(intent);
        }
    }

    private class OnViewEventListener implements View.OnClickListener {

        @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        @Override
        public void onClick(View v) {
            long eventID = 437; // enter valid event id here
            Uri uri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventID);
            Intent intent = new Intent(Intent.ACTION_VIEW).setData(uri);
            startActivity(intent);
        }
    }

    private class OnViewTimeListener implements View.OnClickListener {

        @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        @Override
        public void onClick(View v) {
            long startMillis = System.currentTimeMillis();
            Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
            builder.appendPath("time");
            ContentUris.appendId(builder, startMillis);
            Intent intent = new Intent(Intent.ACTION_VIEW).setData(builder.build());
            startActivity(intent);
        }
    }
}




Java Source Code List

com.roottony.calendar.CalendarContentProviderFragment.java
com.roottony.calendar.CalendarIntentFragment.java
com.roottony.calendar.CalendarUtil.java
com.roottony.calendar.MainActivity.java