Skip to content

Commit

Permalink
Fix #366 - ContextThemeWrapper doesn't use themeResource when inflate…
Browse files Browse the repository at this point in the history
… R.layout.calendar_view
  • Loading branch information
thomasdao committed Feb 16, 2016
1 parent dbdb5cf commit 6d77a15
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 50 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.android.tools.build:gradle:1.5.0'
}
}

Expand Down
51 changes: 27 additions & 24 deletions caldroid/src/main/java/com/roomorama/caldroid/CaldroidFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
Expand Down Expand Up @@ -78,8 +79,6 @@

@SuppressLint("DefaultLocale")
public class CaldroidFragment extends DialogFragment {
public String TAG = "CaldroidFragment";

/**
* Weekday conventions
*/
Expand Down Expand Up @@ -184,7 +183,7 @@ public class CaldroidFragment extends DialogFragment {
/**
* backgroundForDateMap holds background resource for each date
*/
protected Map<DateTime, Integer> backgroundForDateTimeMap = new HashMap<>();
protected Map<DateTime, Drawable> backgroundForDateTimeMap = new HashMap<>();

/**
* textColorForDateMap holds color for text for each date
Expand Down Expand Up @@ -306,7 +305,7 @@ public InfiniteViewPager getDateViewPager() {
/*
* For client to access background and text color maps
*/
public Map<DateTime, Integer> getBackgroundForDateTimeMap() {
public Map<DateTime, Drawable> getBackgroundForDateTimeMap() {
return backgroundForDateTimeMap;
}

Expand Down Expand Up @@ -357,9 +356,8 @@ public Map<String, Object> getCaldroidData() {
caldroidData.put(SELECTED_DATES, selectedDates);
caldroidData.put(_MIN_DATE_TIME, minDateTime);
caldroidData.put(_MAX_DATE_TIME, maxDateTime);
caldroidData.put(START_DAY_OF_WEEK, Integer.valueOf(startDayOfWeek));
caldroidData.put(SIX_WEEKS_IN_CALENDAR,
Boolean.valueOf(sixWeeksInCalendar));
caldroidData.put(START_DAY_OF_WEEK, startDayOfWeek);
caldroidData.put(SIX_WEEKS_IN_CALENDAR, sixWeeksInCalendar);
caldroidData.put(SQUARE_TEXT_VIEW_CELL, squareTextViewCell);
caldroidData.put(THEME_RESOURCE, themeResource);

Expand Down Expand Up @@ -393,60 +391,60 @@ public void setExtraData(Map<String, Object> extraData) {
/**
* Set backgroundForDateMap
*/
public void setBackgroundResourceForDates(
Map<Date, Integer> backgroundForDateMap) {
public void setBackgroundDrawableForDates(
Map<Date, Drawable> backgroundForDateMap) {
if (backgroundForDateMap == null || backgroundForDateMap.size() == 0) {
return;
}

backgroundForDateTimeMap.clear();

for (Date date : backgroundForDateMap.keySet()) {
Integer resource = backgroundForDateMap.get(date);
Drawable drawable = backgroundForDateMap.get(date);
DateTime dateTime = CalendarHelper.convertDateToDateTime(date);
backgroundForDateTimeMap.put(dateTime, resource);
backgroundForDateTimeMap.put(dateTime, drawable);
}
}

public void clearBackgroundResourceForDates(List<Date> dates) {
public void clearBackgroundDrawableForDates(List<Date> dates) {
if (dates == null || dates.size() == 0) {
return;
}

for (Date date : dates) {
clearBackgroundResourceForDate(date);
clearBackgroundDrawableForDate(date);
}
}

public void setBackgroundResourceForDateTimes(
Map<DateTime, Integer> backgroundForDateTimeMap) {
public void setBackgroundDrawableForDateTimes(
Map<DateTime, Drawable> backgroundForDateTimeMap) {
this.backgroundForDateTimeMap.putAll(backgroundForDateTimeMap);
}

public void clearBackgroundResourceForDateTimes(List<DateTime> dateTimes) {
public void clearBackgroundDrawableForDateTimes(List<DateTime> dateTimes) {
if (dateTimes == null || dateTimes.size() == 0) return;

for (DateTime dateTime : dateTimes) {
backgroundForDateTimeMap.remove(dateTime);
}
}

public void setBackgroundResourceForDate(int backgroundRes, Date date) {
public void setBackgroundDrawableForDate(Drawable drawable, Date date) {
DateTime dateTime = CalendarHelper.convertDateToDateTime(date);
backgroundForDateTimeMap.put(dateTime, backgroundRes);
backgroundForDateTimeMap.put(dateTime, drawable);
}

public void clearBackgroundResourceForDate(Date date) {
public void clearBackgroundDrawableForDate(Date date) {
DateTime dateTime = CalendarHelper.convertDateToDateTime(date);
backgroundForDateTimeMap.remove(dateTime);
}

public void setBackgroundResourceForDateTime(int backgroundRes,
public void setBackgroundDrawableForDateTime(Drawable drawable,
DateTime dateTime) {
backgroundForDateTimeMap.put(dateTime, backgroundRes);
backgroundForDateTimeMap.put(dateTime, drawable);
}

public void clearBackgroundResourceForDateTime(DateTime dateTime) {
public void clearBackgroundDrawableForDateTime(DateTime dateTime) {
backgroundForDateTimeMap.remove(dateTime);
}

Expand Down Expand Up @@ -1222,7 +1220,7 @@ public int getThemeResource() {
return themeResource;
}

public static LayoutInflater getLayoutInflater(Context context, LayoutInflater origInflater, int themeResource) {
public static LayoutInflater getThemeInflater(Context context, LayoutInflater origInflater, int themeResource) {
Context wrapped = new ContextThemeWrapper(context, themeResource);
return origInflater.cloneInContext(wrapped);
}
Expand All @@ -1244,7 +1242,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
}
}

LayoutInflater localInflater = getLayoutInflater(getActivity(), inflater, themeResource);
LayoutInflater localInflater = getThemeInflater(getActivity(), inflater, themeResource);

// This is a hack to fix issue localInflater doesn't use the themeResource, make Android
// complain about layout_width and layout_height missing. I'm unsure about its impact
// for app that wants to change theme dynamically.
getActivity().setTheme(themeResource);

View view = localInflater.inflate(R.layout.calendar_view, container, false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.TypedValue;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -154,7 +156,7 @@ public CaldroidGridAdapter(Context context, int month, int year,

LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
localInflater = CaldroidFragment.getLayoutInflater(context, inflater, themeResource);
localInflater = CaldroidFragment.getThemeInflater(context, inflater, themeResource);
}

/**
Expand Down Expand Up @@ -237,24 +239,18 @@ protected DateTime getToday() {
protected void setCustomResources(DateTime dateTime, View backgroundView,
TextView textView) {
// Set custom background resource
Map<DateTime, Integer> backgroundForDateTimeMap = (Map<DateTime, Integer>) caldroidData
Map<DateTime, Drawable> backgroundForDateTimeMap = (Map<DateTime, Drawable>) caldroidData
.get(CaldroidFragment._BACKGROUND_FOR_DATETIME_MAP);
if (backgroundForDateTimeMap != null) {
// Get background resource for the dateTime
Integer backgroundResource = backgroundForDateTimeMap.get(dateTime);
Drawable drawable = backgroundForDateTimeMap.get(dateTime);

// Set it
if (backgroundResource != null) {
try
{
String tmp = this.context.getResources().getResourceName(backgroundResource);
// have resource
backgroundView.setBackgroundResource(backgroundResource);
}
catch(Resources.NotFoundException e)
{
// doesn't have resource, use like color
backgroundView.setBackgroundColor(backgroundResource);
if (drawable != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
backgroundView.setBackground(drawable);
} else {
backgroundView.setBackgroundDrawable(drawable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
}

if (gridView == null) {
LayoutInflater localInflater = CaldroidFragment.getLayoutInflater(getActivity(),
LayoutInflater localInflater = CaldroidFragment.getThemeInflater(getActivity(),
inflater, themeResource);
gridView = (GridView) localInflater.inflate(gridViewRes, container, false);
setupGridView();
Expand Down
2 changes: 1 addition & 1 deletion caldroid/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<item name="styleCaldroidGridView">@style/CaldroidDefaultDarkGridView</item>
</style>

<style name="CaldroidDefaultDarkCalendarViewLayout">
<style name="CaldroidDefaultDarkCalendarViewLayout" parent="CaldroidDefaultCalendarViewLayout">
<item name="android:background">@android:color/black</item>
</style>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.caldroidsample;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
Expand Down Expand Up @@ -37,10 +39,10 @@ private void setCustomResourceForDates() {
Date greenDate = cal.getTime();

if (caldroidFragment != null) {
caldroidFragment.setBackgroundResourceForDate(R.color.blue,
blueDate);
caldroidFragment.setBackgroundResourceForDate(R.color.green,
greenDate);
ColorDrawable blue = new ColorDrawable(getResources().getColor(R.color.blue));
ColorDrawable green = new ColorDrawable(Color.GREEN);
caldroidFragment.setBackgroundDrawableForDate(blue, blueDate);
caldroidFragment.setBackgroundDrawableForDate(green, greenDate);
caldroidFragment.setTextColorForDate(R.color.white, blueDate);
caldroidFragment.setTextColorForDate(R.color.white, greenDate);
}
Expand Down Expand Up @@ -105,9 +107,6 @@ protected void onCreate(Bundle savedInstanceState) {
public void onSelectDate(Date date, View view) {
Toast.makeText(getApplicationContext(), formatter.format(date),
Toast.LENGTH_SHORT).show();

caldroidFragment.setBackgroundResourceForDate(0xffff0000, date);
caldroidFragment.refreshView();
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
VERSION_NAME=2.3.1
VERSION_CODE=2
VERSION_NAME=3.0.0
VERSION_CODE=3
GROUP=com.roomorama

POM_DESCRIPTION=A better calendar for Android
Expand Down

0 comments on commit 6d77a15

Please sign in to comment.