Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit aa12aa4d authored by Gitsaibot's avatar Gitsaibot Committed by GitHub
Browse files

Calendar: Support display lunar/festival when Language is Chinese (#263)

If the current language is set as Chinese,Calendar could display
the lunar and festival for month,week,and day view.

Change-Id: Ie6baff4257939e2a99fc173d57e18651618044b5

(cherry picked from commit 29b4d09)
parent b96b80cd
Loading
Loading
Loading
Loading
+18 −4
Original line number Original line Diff line number Diff line
@@ -14,8 +14,7 @@
     limitations under the License.
     limitations under the License.
-->
-->


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_width="wrap_content"
    android:layout_height="48dip"
    android:layout_height="48dip"
    android:gravity="center_vertical"
    android:gravity="center_vertical"
@@ -29,15 +28,30 @@
        android:textSize="12sp"
        android:textSize="12sp"
        android:layout_marginTop="-2dp"
        android:layout_marginTop="-2dp"
        android:layout_marginBottom="-8dp"
        android:layout_marginBottom="-8dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:textColor="@color/calendar_view_switch_menu_text_color"
        android:gravity="start" />

    <TextView android:id="@+id/top_button_lunar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="-2dip"
        android:layout_marginBottom="-8dip"
        android:layout_marginStart="8dip"
        android:layout_toEndOf="@id/top_button_weekday"
        android:singleLine="true"
        android:textSize="12sp"
        android:textColor="@color/calendar_view_switch_menu_text_color"
        android:textColor="@color/calendar_view_switch_menu_text_color"
        android:gravity="start" />
        android:gravity="start" />


    <TextView android:id="@+id/top_button_date"
    <TextView android:id="@+id/top_button_date"
        android:layout_width="wrap_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@id/top_button_weekday"
        android:singleLine="true"
        android:singleLine="true"
        android:textSize="@dimen/action_bar_date_text_size"
        android:textSize="@dimen/action_bar_date_text_size"
        android:textColor="@color/calendar_view_switch_menu_text_color"
        android:textColor="@color/calendar_view_switch_menu_text_color"
        android:gravity="start" />
        android:gravity="start" />
</LinearLayout>
</RelativeLayout>
+70 −0
Original line number Original line Diff line number Diff line
@@ -16,8 +16,12 @@


package com.android.calendar;
package com.android.calendar;


import com.android.calendar.LunarUtils.LunarInfoLoader;
import android.content.Context;
import android.content.Context;
import android.content.Loader;
import android.content.Loader.OnLoadCompleteListener;
import android.os.Handler;
import android.os.Handler;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.text.format.DateUtils;
import android.text.format.Time;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.LayoutInflater;
@@ -28,6 +32,7 @@ import android.widget.TextView;


import com.android.calendar.CalendarController.ViewType;
import com.android.calendar.CalendarController.ViewType;


import java.util.Calendar;
import java.util.Formatter;
import java.util.Formatter;
import java.util.Locale;
import java.util.Locale;


@@ -69,6 +74,8 @@ public class CalendarViewAdapter extends BaseAdapter {
    private String mTimeZone;
    private String mTimeZone;
    private long mTodayJulianDay;
    private long mTodayJulianDay;
    private Handler mMidnightHandler = null; // Used to run a time update every midnight
    private Handler mMidnightHandler = null; // Used to run a time update every midnight
    private LunarInfoLoader mLunarLoader = null;

    // Updates time specific variables (time-zone, today's Julian day).
    // Updates time specific variables (time-zone, today's Julian day).
    private final Runnable mTimeUpdater = new Runnable() {
    private final Runnable mTimeUpdater = new Runnable() {
        @Override
        @Override
@@ -77,6 +84,13 @@ public class CalendarViewAdapter extends BaseAdapter {
        }
        }
    };
    };


    private OnLoadCompleteListener<Void> mLunarLoaderListener = new OnLoadCompleteListener<Void>() {
        @Override
        public void onLoadComplete(Loader<Void> loader, Void data) {
            notifyDataSetChanged();
        }
    };

    public CalendarViewAdapter(Context context, int viewType, boolean showDate) {
    public CalendarViewAdapter(Context context, int viewType, boolean showDate) {
        super();
        super();


@@ -95,8 +109,19 @@ public class CalendarViewAdapter extends BaseAdapter {
        if (showDate) {
        if (showDate) {
            refresh(context);
            refresh(context);
        }
        }

        mLunarLoader = new LunarInfoLoader(mContext);
        mLunarLoader.registerListener(0, mLunarLoaderListener);
    }
    }


    @Override
    protected void finalize() throws Throwable {
        LunarUtils.clearInfo();
        if (mLunarLoader != null && mLunarLoaderListener != null) {
            mLunarLoader.unregisterListener(mLunarLoaderListener);
        }
        super.finalize();
    }


    // Sets the time zone and today's Julian day to be used by the adapter.
    // Sets the time zone and today's Julian day to be used by the adapter.
    // Also, notify listener on the change and resets the midnight update thread.
    // Also, notify listener on the change and resets the midnight update thread.
@@ -171,15 +196,30 @@ public class CalendarViewAdapter extends BaseAdapter {
                v = convertView;
                v = convertView;
            }
            }
            TextView weekDay = (TextView) v.findViewById(R.id.top_button_weekday);
            TextView weekDay = (TextView) v.findViewById(R.id.top_button_weekday);
            TextView lunarInfo = (TextView) v.findViewById(R.id.top_button_lunar);
            TextView date = (TextView) v.findViewById(R.id.top_button_date);
            TextView date = (TextView) v.findViewById(R.id.top_button_date);


            switch (mCurrentMainView) {
            switch (mCurrentMainView) {
                case ViewType.DAY:
                case ViewType.DAY:
                    weekDay.setVisibility(View.VISIBLE);
                    weekDay.setVisibility(View.VISIBLE);
                    weekDay.setText(buildDayOfWeek());
                    weekDay.setText(buildDayOfWeek());
                    if (LunarUtils.showLunar(mContext)) {
                        lunarInfo.setVisibility(View.VISIBLE);
                        Time time = new Time(mTimeZone);
                        time.set(mMilliTime);
                        int flag = LunarUtils.FORMAT_LUNAR_LONG | LunarUtils.FORMAT_MULTI_FESTIVAL;
                        String lunar = LunarUtils.get(mContext, time.year, time.month,
                                time.monthDay, flag, false, null);
                        if (!TextUtils.isEmpty(lunar)) {
                            lunarInfo.setText(lunar);
                        }
                    } else {
                        lunarInfo.setVisibility(View.GONE);
                    }
                    date.setText(buildFullDate());
                    date.setText(buildFullDate());
                    break;
                    break;
                case ViewType.WEEK:
                case ViewType.WEEK:
                    lunarInfo.setVisibility(View.GONE);
                    if (Utils.getShowWeekNumber(mContext)) {
                    if (Utils.getShowWeekNumber(mContext)) {
                        weekDay.setVisibility(View.VISIBLE);
                        weekDay.setVisibility(View.VISIBLE);
                        weekDay.setText(buildWeekNum());
                        weekDay.setText(buildWeekNum());
@@ -190,10 +230,12 @@ public class CalendarViewAdapter extends BaseAdapter {
                    break;
                    break;
                case ViewType.MONTH:
                case ViewType.MONTH:
                    weekDay.setVisibility(View.GONE);
                    weekDay.setVisibility(View.GONE);
                    lunarInfo.setVisibility(View.GONE);
                    date.setText(buildMonthYearDate());
                    date.setText(buildMonthYearDate());
                    break;
                    break;
                case ViewType.AGENDA:
                case ViewType.AGENDA:
                    weekDay.setVisibility(View.VISIBLE);
                    weekDay.setVisibility(View.VISIBLE);
                    lunarInfo.setVisibility(View.GONE);
                    weekDay.setText(buildDayOfWeek());
                    weekDay.setText(buildDayOfWeek());
                    date.setText(buildFullDate());
                    date.setText(buildFullDate());
                    break;
                    break;
@@ -298,6 +340,9 @@ public class CalendarViewAdapter extends BaseAdapter {
    // Used when the user selects a new day/week/month to watch
    // Used when the user selects a new day/week/month to watch
    public void setTime(long time) {
    public void setTime(long time) {
        mMilliTime = time;
        mMilliTime = time;
        if (LunarUtils.showLunar(mContext)) {
            buildLunarInfo();
        }
        notifyDataSetChanged();
        notifyDataSetChanged();
    }
    }


@@ -415,5 +460,30 @@ public class CalendarViewAdapter extends BaseAdapter {
        return mContext.getResources().getQuantityString(R.plurals.weekN, week, week);
        return mContext.getResources().getQuantityString(R.plurals.weekN, week, week);
    }
    }


    private void buildLunarInfo() {
        if (mLunarLoader == null || TextUtils.isEmpty(mTimeZone)) return;

        Time time = new Time(mTimeZone);
        if (time != null) {
            // The the current month.
            time.set(mMilliTime);

            // As the first day of previous month;
            Calendar from = Calendar.getInstance();
            from.set(time.year, time.month - 1, 1);

            // Get the last day of next month.
            Calendar to = Calendar.getInstance();
            to.set(Calendar.YEAR, time.year);
            to.set(Calendar.MONTH, time.month + 1);
            to.set(Calendar.DAY_OF_MONTH, to.getMaximum(Calendar.DAY_OF_MONTH));

            // Call LunarUtils to load the info.
            mLunarLoader.load(from.get(Calendar.YEAR), from.get(Calendar.MONTH),
                    from.get(Calendar.DAY_OF_MONTH), to.get(Calendar.YEAR), to.get(Calendar.MONTH),
                    to.get(Calendar.DAY_OF_MONTH));
        }
    }

}
}
+31 −2
Original line number Original line Diff line number Diff line
@@ -652,6 +652,10 @@ public class DayView extends View implements View.OnCreateContextMenuListener,
        }
        }
        HOURS_MARGIN = HOURS_LEFT_MARGIN + HOURS_RIGHT_MARGIN;
        HOURS_MARGIN = HOURS_LEFT_MARGIN + HOURS_RIGHT_MARGIN;
        DAY_HEADER_HEIGHT = mNumDays == 1 ? ONE_DAY_HEADER_HEIGHT : MULTI_DAY_HEADER_HEIGHT;
        DAY_HEADER_HEIGHT = mNumDays == 1 ? ONE_DAY_HEADER_HEIGHT : MULTI_DAY_HEADER_HEIGHT;
        if (LunarUtils.showLunar(mContext) && mNumDays != 1) {
            DAY_HEADER_HEIGHT = (int) (DAY_HEADER_HEIGHT + DAY_HEADER_FONT_SIZE + 2);
        }

        mCurrentTimeLine = mResources.getDrawable(R.drawable.timeline_indicator_holo_light);
        mCurrentTimeLine = mResources.getDrawable(R.drawable.timeline_indicator_holo_light);
        mCurrentTimeAnimateLine = mResources
        mCurrentTimeAnimateLine = mResources
                .getDrawable(R.drawable.timeline_indicator_activated_holo_light);
                .getDrawable(R.drawable.timeline_indicator_activated_holo_light);
@@ -2503,8 +2507,12 @@ public class DayView extends View implements View.OnCreateContextMenuListener,
        // Draw day of the month
        // Draw day of the month
        String dateNumStr = String.valueOf(dateNum);
        String dateNumStr = String.valueOf(dateNum);
        if (mNumDays > 1) {
        if (mNumDays > 1) {
            float y = DAY_HEADER_HEIGHT - DAY_HEADER_BOTTOM_MARGIN;
            float y = -1;

            if (LunarUtils.showLunar(mContext)) {
                y = DAY_HEADER_HEIGHT - DAY_HEADER_BOTTOM_MARGIN - DATE_HEADER_FONT_SIZE - 2;
            } else {
                y = DAY_HEADER_HEIGHT - DAY_HEADER_BOTTOM_MARGIN;
            }
            // Draw day of the month
            // Draw day of the month
            x = computeDayLeftPosition(day) + DAY_HEADER_RIGHT_MARGIN;
            x = computeDayLeftPosition(day) + DAY_HEADER_RIGHT_MARGIN;
            p.setTextAlign(Align.LEFT);
            p.setTextAlign(Align.LEFT);
@@ -2519,6 +2527,27 @@ public class DayView extends View implements View.OnCreateContextMenuListener,
            p.setTextSize(DAY_HEADER_FONT_SIZE);
            p.setTextSize(DAY_HEADER_FONT_SIZE);
            p.setTypeface(Typeface.DEFAULT);
            p.setTypeface(Typeface.DEFAULT);
            canvas.drawText(dayStr, x, y, p);
            canvas.drawText(dayStr, x, y, p);

            // To show the lunar info.
            if (LunarUtils.showLunar(mContext)) {
                // adjust the year and month
                int month = mBaseDate.month;
                int year = mBaseDate.year;
                if (dateNum > mMonthLength || dateNum < mFirstVisibleDate) {
                    month = month + 1;
                    if (month > 11) {
                        month = 0;
                        year = year + 1;
                    }
                }

                String lunarInfo = LunarUtils.get(mContext, year, month, dateNum,
                        LunarUtils.FORMAT_LUNAR_SHORT | LunarUtils.FORMAT_ONE_FESTIVAL,
                        false, null);
                if (!TextUtils.isEmpty(lunarInfo)) {
                    canvas.drawText(lunarInfo, x, y + DAY_HEADER_FONT_SIZE + 2, p);
                }
            }
        } else {
        } else {
            float y = ONE_DAY_HEADER_HEIGHT - DAY_HEADER_ONE_DAY_BOTTOM_MARGIN;
            float y = ONE_DAY_HEADER_HEIGHT - DAY_HEADER_ONE_DAY_BOTTOM_MARGIN;
            p.setTextAlign(Align.LEFT);
            p.setTextAlign(Align.LEFT);
+305 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *     Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *     Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *     Neither the name of The Linux Foundation nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.android.calendar;

import android.content.AsyncTaskLoader;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;

public class LunarUtils {
    private static final String TAG = "LunarUtils";

    // The flags used for get the lunar info.
    public static final int FORMAT_LUNAR_LONG = 0x00001;
    public static final int FORMAT_LUNAR_SHORT = 0x00002;
    public static final int FORMAT_ONE_FESTIVAL = 0x00004;
    public static final int FORMAT_MULTI_FESTIVAL = 0x00008;
    public static final int FORMAT_ANIMAL = 0x00010;

    private static final String INFO_SEPARATE = " ";
    private static final String MORE_FESTIVAL_SUFFIX = "*";

    private static HashMap<String, LunarInfo> sLunarInfos = new HashMap<String, LunarInfo>();

    /**
     * If need show the lunar info now. As default, it will need shown if the current
     * language is zh-cn.
     */
    public static boolean showLunar(Context context) {
        Locale locale = Locale.getDefault();
        String language = locale.getLanguage().toLowerCase();
        String country = locale.getCountry().toLowerCase();
        return ("zh".equals(language) && "cn".equals(country));
    }

    /**
     * Used to clear the saved info.
     */
    public static void clearInfo() {
        Log.i(TAG, "Clear all the saved info.");
        sLunarInfos.clear();
    }

    /**
     * Used to get the lunar, festival and animal info of the date. Before you call this
     * function to get the info, you need make sure already load the info by calling
     * {@link LunarInfoLoader#load} to pre-load them.
     * @param format Format which info need append to the result.
     *     The format {@link #FORMAT_LUNAR_LONG} and {@link #FORMAT_LUNAR_SHORT},
     *     {@link #FORMAT_ONE_FESTIVAL} and {@link #FORMAT_MULTI_FESTIVAL} could not
     *     selected at once.
     * @param showLunarBeforeFestival If the festival is exist for the date, if need append the
     *     lunar info before the festival info.
     * @param result [out] The result will be saved in this list as your given format.
     * @return The result as string for your given format.
     */
    public static String get(Context context, int year, int month, int day, int format,
            boolean showLunarBeforeFestival, ArrayList<String> result) {
        if (context == null || format < FORMAT_LUNAR_LONG) return null;

        String res = null;

        // Try to find the matched lunar info from the hash map.
        String key = getKey(year, month, day);
        LunarInfo info = sLunarInfos.get(key);
        if (info != null) {
            res = buildInfo(info, format, showLunarBeforeFestival, result);
        } else {
            Log.d(TAG, "Couldn't get the lunar info for " + key);
        }

        return res;
    }

    private static String getKey(int year, int month, int day) {
        return year + "-" + month + "-" + day;
    }

    private static String buildInfo(LunarInfo info, int format, boolean showLunarBeforeFestival,
            ArrayList<String> list) {
        if (info == null || format < FORMAT_LUNAR_LONG) return null;

        StringBuilder result = new StringBuilder();

        if (showLunarBeforeFestival || TextUtils.isEmpty(info._festival1)) {
            // The format should not support long and short at one time.
            if ((format & FORMAT_LUNAR_LONG) == FORMAT_LUNAR_LONG) {
                appendInfo(result, info._label_long, list);
            } else if ((format & FORMAT_LUNAR_SHORT) == FORMAT_LUNAR_SHORT) {
                appendInfo(result, info._label_short, list);
            }
        }

        // The format should not support only one festival and multiple festivals.
        if ((format & FORMAT_ONE_FESTIVAL) == FORMAT_ONE_FESTIVAL) {
            String festival = info._festival1;
            if (!TextUtils.isEmpty(info._festival2)) {
                festival = festival + MORE_FESTIVAL_SUFFIX;
            }
            appendInfo(result, festival, list);
        } else if ((format & FORMAT_MULTI_FESTIVAL) == FORMAT_MULTI_FESTIVAL) {
            appendInfo(result, info._festival1, list);
            appendInfo(result, info._festival2, list);
            appendInfo(result, info._festival3, list);
            appendInfo(result, info._festival4, list);
        }

        if ((format & FORMAT_ANIMAL) == FORMAT_ANIMAL) {
            appendInfo(result, info._animal, list);
        }

        return result.toString();
    }

    private static void appendInfo(StringBuilder builder, String info, ArrayList<String> list) {
        if (builder == null || TextUtils.isEmpty(info)) return;

        String prefix = builder.length() > 0 ? INFO_SEPARATE : "";
        builder.append(prefix).append(info);

        if (list != null) list.add(info);
    }

    public static class LunarInfoLoader extends AsyncTaskLoader<Void> {
        private static final Uri CONTENT_URI_GET_ONE_DAY =
                Uri.parse("content://com.qualcomm.qti.lunarinfo/one_day");
        private static final Uri CONTENT_URI_GET_ONE_MONTH =
                Uri.parse("content://com.qualcomm.qti.lunarinfo/one_month");
        private static final Uri CONTENT_URI_GET_FROM_TO =
                Uri.parse("content://com.qualcomm.qti.lunarinfo/from_to");

        // The query parameters used to get lunar info.
        private static final String PARAM_YEAR = "year";
        private static final String PARAM_MONTH = "month";
        private static final String PARAM_DAY = "day";
        private static final String PARAM_FROM_YEAR = "from_year";
        private static final String PARAM_FROM_MONTH = "from_month";
        private static final String PARAM_FROM_DAY = "from_day";
        private static final String PARAM_TO_YEAR = "to_year";
        private static final String PARAM_TO_MONTH = "to_month";
        private static final String PARAM_TO_DAY = "to_day";

        // The columns for result.
        private static final String COL_ID = "_id";
        private static final String COL_YEAR = "year";
        private static final String COL_MONTH = "month";
        private static final String COL_DAY = "day";
        private static final String COL_LUNAR_LABEL_LONG = "lunar_label_long";
        private static final String COL_LUNAR_LABEL_SHORT = "lunar_label_short";
        private static final String COL_ANIMAL = "animal";
        private static final String COL_FESTIVAL_1 = "festival_1";
        private static final String COL_FESTIVAL_2 = "festival_2";
        private static final String COL_FESTIVAL_3 = "festival_3";
        private static final String COL_FESTIVAL_4 = "festival_4";

        private static int sIndexId = -1;
        private static int sIndexYear = -1;
        private static int sIndexMonth = -1;
        private static int sIndexDay = -1;
        private static int sIndexLunarLabelLong = -1;
        private static int sIndexLunarLabelShort = -1;
        private static int sIndexAnimal = -1;
        private static int sIndexFestival1 = -1;
        private static int sIndexFestival2 = -1;
        private static int sIndexFestival3 = -1;
        private static int sIndexFestival4 = -1;

        private Uri mUri;

        public LunarInfoLoader(Context context) {
            super(context);
        }

        public void load(int year, int month, int day) {
            reset();
            // Build the query uri.
            mUri = CONTENT_URI_GET_ONE_DAY.buildUpon()
                    .appendQueryParameter(PARAM_YEAR, String.valueOf(year))
                    .appendQueryParameter(PARAM_MONTH, String.valueOf(month))
                    .appendQueryParameter(PARAM_DAY, String.valueOf(day))
                    .build();
            startLoading();
            forceLoad();
        }

        public void load(int year, int month) {
            reset();
            // Build the query uri.
            mUri = CONTENT_URI_GET_ONE_MONTH.buildUpon()
                    .appendQueryParameter(PARAM_YEAR, String.valueOf(year))
                    .appendQueryParameter(PARAM_MONTH, String.valueOf(month))
                    .build();
            startLoading();
            forceLoad();
        }

        public void load(int from_year, int from_month, int from_day,
                int to_year, int to_month, int to_day) {
            reset();
            // Build the query uri.
            mUri = CONTENT_URI_GET_FROM_TO.buildUpon()
                    .appendQueryParameter(PARAM_FROM_YEAR, String.valueOf(from_year))
                    .appendQueryParameter(PARAM_FROM_MONTH, String.valueOf(from_month))
                    .appendQueryParameter(PARAM_FROM_DAY, String.valueOf(from_day))
                    .appendQueryParameter(PARAM_TO_YEAR, String.valueOf(to_year))
                    .appendQueryParameter(PARAM_TO_MONTH, String.valueOf(to_month))
                    .appendQueryParameter(PARAM_TO_DAY, String.valueOf(to_day))
                    .build();
            startLoading();
            forceLoad();
        }

        @Override
        public Void loadInBackground() {
            Cursor cursor = getContext().getContentResolver().query(mUri, null, null, null, null);
            try {
                if (cursor == null || cursor.getCount() < 1) return null;

                if (sIndexId < 0) getIndexValue(cursor);
                while (cursor.moveToNext()) {
                    int year = cursor.getInt(sIndexYear);
                    int month = cursor.getInt(sIndexMonth);
                    int day = cursor.getInt(sIndexDay);

                    LunarInfo info = new LunarInfo();
                    info._label_long = cursor.getString(sIndexLunarLabelLong);
                    info._label_short = cursor.getString(sIndexLunarLabelShort);
                    info._animal = cursor.getString(sIndexAnimal);
                    info._festival1 = cursor.getString(sIndexFestival1);
                    info._festival2 = cursor.getString(sIndexFestival2);
                    info._festival3 = cursor.getString(sIndexFestival3);
                    info._festival4 = cursor.getString(sIndexFestival4);

                    sLunarInfos.put(getKey(year, month, day), info);
                }
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }

            return null;
        }

        private void getIndexValue(Cursor cursor) {
            if (cursor == null) return;

            sIndexId = cursor.getColumnIndexOrThrow(COL_ID);
            sIndexYear = cursor.getColumnIndexOrThrow(COL_YEAR);
            sIndexMonth = cursor.getColumnIndexOrThrow(COL_MONTH);
            sIndexDay = cursor.getColumnIndexOrThrow(COL_DAY);
            sIndexLunarLabelLong = cursor.getColumnIndexOrThrow(COL_LUNAR_LABEL_LONG);
            sIndexLunarLabelShort = cursor.getColumnIndexOrThrow(COL_LUNAR_LABEL_SHORT);
            sIndexAnimal = cursor.getColumnIndexOrThrow(COL_ANIMAL);
            sIndexFestival1 = cursor.getColumnIndexOrThrow(COL_FESTIVAL_1);
            sIndexFestival2 = cursor.getColumnIndexOrThrow(COL_FESTIVAL_2);
            sIndexFestival3 = cursor.getColumnIndexOrThrow(COL_FESTIVAL_3);
            sIndexFestival4 = cursor.getColumnIndexOrThrow(COL_FESTIVAL_4);
        }

    }

    private static class LunarInfo {
        public String _label_long;
        public String _label_short;
        public String _animal;
        public String _festival1;
        public String _festival2;
        public String _festival3;
        public String _festival4;
    }
}
+68 −3

File changed.

Preview size limit exceeded, changes collapsed.