Android Open Source - o365api-android-get-events-app Helpers






From Project

Back to project page o365api-android-get-events-app.

License

The source code is released under:

MIT License

If you think the Android project o365api-android-get-events-app 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.example.mattleib.myinboxapplication;
/*ww w .  j av  a2  s  .co m*/
import android.content.SharedPreferences;
import android.text.format.Time;
import android.util.Log;

import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 * Created by mattleib on 1/28/2015.
 */
public class Helpers {

    public static String LogEnterMethod(String methodName)
    {
        return "*Entering [" + methodName + "]";
    }

    public static String LogLeaveMethod(String methodName)
    {
        return "*Leaving [" + methodName + "]";
    }

    public static String LogInMethod(String methodName)
    {
        return "**Working in [" + methodName + "]";
    }

    public static void LogIfNull(String tag, Object obj, String objectName)
    {
        if(obj == null){
            Log.d(tag, "NullObject::" + objectName);
        }
    }

    public static JSONObject TryGetJSONObject(JSONObject sourceObject, String name)
    {
        try {
            JSONObject obj = sourceObject.getJSONObject(name);
            return obj;
        }
        catch(Exception e){
            return null;
        }
    }

    public static String TryGetJSONValue(JSONObject sourceObject, String name)
    {
        try {
            String value = sourceObject.getString(name);
            return value;
        }
        catch(Exception e){
            return "";
        }
    }

    public static String GetEventsQueryString(String queryTemplate,
                                              DataTypes.EventTimeSpan eventTimeSpan,
                                              Boolean doNotShowPastEvents) {
        //"Start": "2015-01-23T20:00:00Z",
        //"End": "2015-01-23T21:00:00Z",
        // Get the local time
        Time localTimeNow = new Time(Time.getCurrentTimezone()); // returns time in current TimeZone
        localTimeNow.setToNow(); // set it to now
        long nowLocalTimeMilliseconds = localTimeNow.toMillis(false);

        if(!doNotShowPastEvents) { // get rid of hours, midnight today to start with all events of today
            localTimeNow.set(localTimeNow.monthDay, localTimeNow.month, localTimeNow.year);
        } else { // keep only events starting previous 60 minutes from now
            localTimeNow.set(nowLocalTimeMilliseconds - (Constants.OneMinuteInMilliseconds * 60));
        }

        Calendar cal = Calendar.getInstance();
        cal.set(localTimeNow.year, localTimeNow.month, localTimeNow.monthDay, localTimeNow.hour, localTimeNow.minute, localTimeNow.second);
        // convert to UTC
        int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
        int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
        cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
        Date startDate = cal.getTime();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        String startDateTime = fmt.format(startDate);

        if (eventTimeSpan == DataTypes.EventTimeSpan.Day) {
            cal.add(Calendar.HOUR, 24);
        } else if (eventTimeSpan == DataTypes.EventTimeSpan.Week) {
            cal.add(Calendar.HOUR, 24 * 7);
        } else { // 30 days
            cal.add(Calendar.HOUR, 24 * 30);
        }

        Date endDate = cal.getTime();
        String endDateTime = fmt.format(endDate);

        String queryString = String.format(queryTemplate, startDateTime, endDateTime);

        return queryString;
    }

    public static boolean IsEventNow(String startTimeUtc, String endTimeUtc) {
        //RFC3339
        //"Start": "2015-01-23T20:00:00Z",
        //"End": "2015-01-23T21:00:00Z",
        Time startTime = new Time();
        startTime.parse3339(startTimeUtc);
        long startTimeMilli = startTime.toMillis(false);
        startTimeMilli = startTimeMilli - (Constants.OneMinuteInMilliseconds * 15); //Give 15 minute buffer
        startTime.set(startTimeMilli);

        Time endTime = new Time();
        endTime.parse3339(endTimeUtc);

        Time now = new Time();
        now.setToNow();

        if(startTime.toMillis(true) < now.toMillis(true) &&
                now.toMillis(true) < endTime.toMillis(true)) {
            return true;
        }

        return false;
    }
}




Java Source Code List

com.example.mattleib.myinboxapplication.AppConfig.java
com.example.mattleib.myinboxapplication.AppHelper.java
com.example.mattleib.myinboxapplication.ApplicationTest.java
com.example.mattleib.myinboxapplication.Constants.java
com.example.mattleib.myinboxapplication.DataTypes.java
com.example.mattleib.myinboxapplication.EmailAddress.java
com.example.mattleib.myinboxapplication.EmptyItem.java
com.example.mattleib.myinboxapplication.EventItem.java
com.example.mattleib.myinboxapplication.EventItemsFragment.java
com.example.mattleib.myinboxapplication.Helpers.java
com.example.mattleib.myinboxapplication.InMemoryCacheStore.java
com.example.mattleib.myinboxapplication.Item.java
com.example.mattleib.myinboxapplication.LocalDateTimeConverter.java
com.example.mattleib.myinboxapplication.Location.java
com.example.mattleib.myinboxapplication.MainActivity.java
com.example.mattleib.myinboxapplication.Organizer.java
com.example.mattleib.myinboxapplication.PreferenceSetting.java
com.example.mattleib.myinboxapplication.PreferenceSettings.java
com.example.mattleib.myinboxapplication.SectionItem.java
com.example.mattleib.myinboxapplication.SettingsActivity.java
com.example.mattleib.myinboxapplication.SettingsFragment.java
com.example.mattleib.myinboxapplication.SimpleAlertDialog.java