Java tutorial
/* * Copyright (C) 2015 puku0x * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.pukulab.puku0x.gscalendar; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.graphics.Typeface; import android.os.CountDownTimer; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBar; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.format.DateFormat; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import com.vdesmet.lib.calendar.DayAdapter; import com.vdesmet.lib.calendar.MultiCalendarView; import com.vdesmet.lib.calendar.OnDayClickListener; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Locale; public class CalendarActivity extends AppCompatActivity implements OnDayClickListener { // ? ProgressBar mProgressBar; // ?? UserData mLoginUser; // ?? UserData mDisplayedUser; // ? //Date mDisplayedDate; // ? private Date mDisplayedDate, mLastDisplayedDate; private Date mStartDate, mEndDate; // ??? private int mViewPagerPosition = 0; // ????? Schedule mLastSchedule; // private List<ScheduleData> mScheduleDataList; // 1 private List<Schedule> mDailyScheduleList; private ListView mDailyScheduleListView; private ScheduleDataAdapter mDailyScheduleaListAdapter; private GetSchedulesTask mScheduleDataTask; // private MultiCalendarView mCalendarView; private TextView mSelectedTextView; private Typeface mSelectedTypeface; // ? private boolean mBackKeyPressed = false; private CountDownTimer mBackKeyTimer; private Toast mToast; // private final static String ARGS_LOGIN_USER = "LOGIN_USER"; private final static String ARGS_DISPLAYED_USER = "DISPLAYED_USER"; private final static String ARGS_DISPLAYED_DATE = "DISPLAYED_DATE"; /** * ?? */ public static Intent createIntent(Context context, UserData loginUser, UserData displayedUser, Date displayedDate) { Intent intent = new Intent(context, CalendarActivity.class); intent.putExtra(ARGS_LOGIN_USER, loginUser); intent.putExtra(ARGS_DISPLAYED_USER, displayedUser); intent.putExtra(ARGS_DISPLAYED_DATE, displayedDate); return intent; } public class CustomDayAdapter implements DayAdapter { public CustomDayAdapter() { } /** * ? */ @Override public int[] getCategoryColors(final long dayInMillis) { // Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(dayInMillis); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); Date date = calendar.getTime(); if (mScheduleDataList != null) { for (ScheduleData d : mScheduleDataList) { if (d.date.equals(date)) { //System.out.println("date = " + d.date); List<Integer> colorList = new ArrayList<>(); for (Schedule s : d.scheduleList) { int color = GroupSessionApi.title_colors[s.color]; if (!colorList.contains(color)) { colorList.add(color); } } // int???? int[] intArray = new int[colorList.size()]; for (int i = 0; i < colorList.size(); i++) { intArray[i] = colorList.get(i); } return intArray; } } } return null; } /** * ? */ @Override public boolean isDayEnabled(final long dayInMillis) { return true; } /** * ? */ @Override public void updateTextView(final TextView dateTextView, final long dayInMillis) { } /** * ? */ @Override public void updateHeaderTextView(final TextView header, final int dayOfWeek) { switch (dayOfWeek) { case Calendar.SATURDAY: header.setTextColor(Color.BLUE); break; case Calendar.SUNDAY: header.setTextColor(Color.RED); break; default: header.setTextColor(Color.BLACK); break; } } } // private class DisplaySchedulesTask extends GetSchedulesTask { public DisplaySchedulesTask(Context context, UserData displayedUser, Date displayedDate) { super(context, displayedUser); } /** * ?? */ @Override protected void onPreExecute() { super.onPreExecute(); mProgressBar.setVisibility(View.VISIBLE); } /** * ?? */ @Override protected void onPostExecute(List<ScheduleData> data) { super.onPostExecute(data); mProgressBar.setVisibility(View.GONE); if (data != null) { // mScheduleDataList = data; // ?????? mCalendarView.getIndicator().setOnPageChangeListener(null); mCalendarView.getIndicator().setCurrentItem(mViewPagerPosition); mCalendarView.getIndicator().setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { // ? mDailyScheduleList.clear(); mDailyScheduleaListAdapter.notifyDataSetChanged(); // 1??? final Calendar cal = Calendar.getInstance(); cal.setTimeInMillis((mCalendarView.getFirstValidDay().getTimeInMillis())); cal.add(Calendar.MONTH, position); mStartDate = cal.getTime(); cal.add(Calendar.MONTH, 1); mEndDate = cal.getTime(); mViewPagerPosition = position; new DisplaySchedulesTask(CalendarActivity.this, mDisplayedUser, mDisplayedDate) .execute(mStartDate, mEndDate); } @Override public void onPageScrollStateChanged(int state) { } }); try { // ?????? for (final ScheduleData scheduleData : mScheduleDataList) { for (final Schedule schedule : scheduleData.scheduleList) { SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd", Locale.JAPAN); Date start = f.parse(f.format(schedule.start)); Date end = f.parse(f.format(schedule.end)); // ?????? if (end.after(start)) { // ?? for (final ScheduleData scheduleData2 : mScheduleDataList) { Date date = f.parse(f.format(scheduleData2.date)); // ~?? if (date.after(start) && (date.before(end) || date.equals(end))) { // ?? if (!scheduleData2.scheduleList.contains(schedule)) { scheduleData2.scheduleList.add(schedule); } } } } } } } catch (ParseException e) { e.printStackTrace(); } // ? for (ScheduleData sd : mScheduleDataList) { long dayInMillis = sd.date.getTime(); final LinearLayout categories = mCalendarView.getCategoryViewForDate(dayInMillis); if (categories != null) { // categories.removeAllViews(); // ? CustomDayAdapter tmpAdapter = new CustomDayAdapter(); int[] colors = tmpAdapter.getCategoryColors(dayInMillis); for (final int color : colors) { final LayoutInflater inflater = (LayoutInflater) CalendarActivity.this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); final View category = inflater.inflate( com.vdesmet.lib.calendar.R.layout.lib_calendar_category, categories, false); category.setBackgroundColor(color); categories.addView(category); } } } // ?? Calendar calendar = Calendar.getInstance(); calendar.setTime(mDisplayedDate); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); onDayClick(calendar.getTimeInMillis()); } else { Toast.makeText(CalendarActivity.this, getString(R.string.connection_error), Toast.LENGTH_SHORT) .show(); } } } // public class ScheduleDataAdapter extends ArrayAdapter<Schedule> { private LayoutInflater layoutInflater; public ScheduleDataAdapter(Context context, int textViewResourceId, List<Schedule> objects) { super(context, textViewResourceId, objects); layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public View getView(int position, View convertView, ViewGroup parent) { // ?(position)? final Schedule schedule = (Schedule) getItem(position); convertView = layoutInflater.inflate(R.layout.daily_schedule_row, null); // TextView title = (TextView) convertView.findViewById(R.id.tv_schedule_title); title.setText(schedule.title); // (HH:mm) title = (TextView) convertView.findViewById(R.id.tv_schedule_time_start); title.setText(DateFormat.format(getString(R.string.date_hour_minute), schedule.start)); title = (TextView) convertView.findViewById(R.id.tv_schedule_time_end); title.setText(DateFormat.format(getString(R.string.date_hour_minute), schedule.end)); // ImageView bar = (ImageView) convertView.findViewById(R.id.imageView); bar.setBackgroundColor(GroupSessionApi.title_colors[schedule.color]); // ????? String summary = ""; TextView tv_same_schedule_users = (TextView) convertView.findViewById(R.id.tv_schedule_same_users); if (!schedule.sameScheduleUsers.isEmpty()) { for (UserData user : schedule.sameScheduleUsers) { if (!summary.isEmpty()) summary += ", "; summary += user.name; } tv_same_schedule_users.setText(summary); } // ?? if (!schedule.reservedFacilities.isEmpty()) { for (FacilityData facility : schedule.reservedFacilities) { if (!summary.isEmpty()) summary += ", "; summary += facility.name; } tv_same_schedule_users.setText(summary); } return convertView; } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_calendar); // ? mBackKeyTimer = new CountDownTimer(2000, 2000) { @Override public void onTick(long millisUntilFinished) { } @Override public void onFinish() { mBackKeyPressed = false; } }; // ? Bundle extras = getIntent().getExtras(); // ? if (mLoginUser == null) { mLoginUser = (UserData) extras.getSerializable(ARGS_LOGIN_USER); } // if (mDisplayedUser == null) { mDisplayedUser = (UserData) extras.getSerializable(ARGS_DISPLAYED_USER); } // ? if (mDisplayedDate == null) { mDisplayedDate = (Date) extras.getSerializable(ARGS_DISPLAYED_DATE); mLastDisplayedDate = mDisplayedDate; } // ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { // ????????? if (mDisplayedUser.usid != null && !mDisplayedUser.usid.equals(mLoginUser.usid)) { actionBar.setDisplayHomeAsUpEnabled(true); } // actionBar.setTitle(mDisplayedUser.name); actionBar.setSubtitle(DateFormat.format(getString(R.string.date_year_month_day), mDisplayedDate)); } // Retrieve the CalendarView mCalendarView = (MultiCalendarView) findViewById(R.id.calendarView); mProgressBar = (ProgressBar) findViewById(R.id.pb_detail); //mScheduleLayout = (LinearLayout) findViewById(R.id.scheduleList); // ? mScheduleDataList = new ArrayList<>(); mDailyScheduleList = new ArrayList<>(); mDailyScheduleaListAdapter = new ScheduleDataAdapter(CalendarActivity.this, 0, mDailyScheduleList); mDailyScheduleListView = (ListView) findViewById(R.id.lv_schedule); mDailyScheduleListView.setAdapter(mDailyScheduleaListAdapter); mDailyScheduleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ListView listView = (ListView) parent; Schedule schedule = (Schedule) listView.getItemAtPosition(position); // ??? mLastSchedule = new Schedule(schedule); // ?? Intent intent = DetailActivity.createIntent(CalendarActivity.this, mLoginUser, mDisplayedUser, schedule); int requestCode = DetailActivity.REQUEST_DETAIL; startActivityForResult(intent, requestCode); } }); // Set the first valid day final Calendar firstValidDay = Calendar.getInstance(); firstValidDay.add(Calendar.YEAR, -1); firstValidDay.set(Calendar.DAY_OF_MONTH, 1); firstValidDay.set(Calendar.HOUR_OF_DAY, 0); firstValidDay.set(Calendar.MINUTE, 0); firstValidDay.set(Calendar.SECOND, 0); firstValidDay.set(Calendar.MILLISECOND, 0); mCalendarView.setFirstValidDay(firstValidDay); // Set the last valid day final Calendar lastValidDay = Calendar.getInstance(); lastValidDay.add(Calendar.YEAR, 1); lastValidDay.set(Calendar.HOUR_OF_DAY, 0); lastValidDay.set(Calendar.MINUTE, 0); lastValidDay.set(Calendar.SECOND, 0); lastValidDay.set(Calendar.MILLISECOND, 0); mCalendarView.setLastValidDay(lastValidDay); // ~ mCalendarView.setFirstDayOfWeek(Calendar.SUNDAY); mCalendarView.setLastDayOfWeek(Calendar.SATURDAY); // Create adapter final CustomDayAdapter adapter = new CustomDayAdapter(); // Set listener and adapter mCalendarView.setOnDayClickListener(this); mCalendarView.getIndicator().setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { //System.out.println("position:" + position); final Calendar cal = Calendar.getInstance(); cal.setTimeInMillis((mCalendarView.getFirstValidDay().getTimeInMillis())); cal.add(Calendar.MONTH, position); mStartDate = cal.getTime(); cal.add(Calendar.MONTH, 1); mEndDate = cal.getTime(); mViewPagerPosition = position; new DisplaySchedulesTask(CalendarActivity.this, mDisplayedUser, mDisplayedDate).execute(mStartDate, mEndDate); } @Override public void onPageScrollStateChanged(int state) { } }); mCalendarView.setDayAdapter(adapter); // ???????? mCalendarView.setViewPagerPosition(12); } @Override public void onDayClick(final long dayInMillis) { // Reset the previously selected TextView to his previous Typeface if (mSelectedTextView != null) { mSelectedTextView.setTypeface(mSelectedTypeface); mSelectedTextView.setBackgroundColor(getResources().getColor(android.R.color.background_light)); } final TextView day = mCalendarView.getTextViewForDate(dayInMillis); if (day != null) { // Remember the selected TextView and it's font mSelectedTypeface = day.getTypeface(); mSelectedTextView = day; // Show the selected TextView as bold day.setTypeface(Typeface.DEFAULT_BOLD); day.setBackgroundColor(getResources().getColor(R.color.theme_color_transparent)); } // ? Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(dayInMillis); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); mDisplayedDate = calendar.getTime(); // ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { //actionBar.setDisplayHomeAsUpEnabled(true); //actionBar.setTitle(mDisplayedUser.name); actionBar.setSubtitle(DateFormat.format(getString(R.string.date_year_month_day), mDisplayedDate)); } // ? mDailyScheduleList.clear(); for (ScheduleData d : mScheduleDataList) { if (d.date.equals(mDisplayedDate)) { // //TextView tv_date = (TextView) findViewById(R.id.tv_schedule_date); //tv_date.setText(DateFormat.format(getString(R.string.date_month_day_week), d.date)); // ???? if (!d.scheduleList.isEmpty()) { mDailyScheduleList.addAll(d.scheduleList); } } } mDailyScheduleaListAdapter.notifyDataSetChanged(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { // ?? case DetailActivity.REQUEST_DETAIL: // ??? if (resultCode == RESULT_OK) { if (data != null) { // ?? Bundle bundle = data.getExtras(); Schedule newSchedule = (Schedule) bundle.getSerializable(EditActivity.ARGS_NEW_SCHEDULE); // ???????? if (!newSchedule.equals(mLastSchedule)) { // ??? new DisplaySchedulesTask(CalendarActivity.this, mDisplayedUser, mDisplayedDate) .execute(mStartDate, mEndDate); } } } break; // ?? case EditActivity.REQUEST_ADD_SCHEDULE: if (resultCode == RESULT_OK) { if (data != null) { // ?? Bundle bundle = data.getExtras(); Schedule newSchedule = (Schedule) bundle.getSerializable(EditActivity.ARGS_NEW_SCHEDULE); if (newSchedule != null) { } // ??? new DisplaySchedulesTask(CalendarActivity.this, mDisplayedUser, mDisplayedDate) .execute(mStartDate, mEndDate); } } default: break; } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_calendar, menu); // ??????? if (mDisplayedUser.usid != null && !mDisplayedUser.usid.equals(mLoginUser.usid)) { // ?? MenuItem menuAdd = menu.findItem(R.id.menu_add); menuAdd.setVisible(false); // ? invalidateOptionsMenu(); } return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); // if (id == R.id.action_settings) { Intent intent = SettingsActivity.createIntent(CalendarActivity.this); startActivity(intent); return true; } else if (id == R.id.action_today) { // ?? mCalendarView.getIndicator().setCurrentItem(12); // ?? Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); onDayClick(calendar.getTimeInMillis()); } // else if (id == R.id.action_users) { // ????????? if (mDisplayedUser.usid.equals(mLoginUser.usid)) { Intent intent = UserListActivity.createIntent(CalendarActivity.this, mLoginUser, mDisplayedUser, mLastDisplayedDate); int requestCode = UserListActivity.REQUEST_GET_USER; startActivityForResult(intent, requestCode); } else { finish(); } return true; } // else if (id == R.id.menu_refresh) { // ??? new DisplaySchedulesTask(CalendarActivity.this, mDisplayedUser, mDisplayedDate).execute(mStartDate, mEndDate); Toast.makeText(CalendarActivity.this, getString(R.string.refresh), Toast.LENGTH_SHORT).show(); return true; } // else if (id == R.id.menu_add) { // ? Schedule newSchedule = new Schedule(); Calendar cal = Calendar.getInstance(); cal.setTime(mDisplayedDate); cal.set(Calendar.HOUR_OF_DAY, Schedule.DEFAULT_START_HOUR); cal.set(Calendar.MINUTE, Schedule.DEFAULT_START_MINUTE); newSchedule.start = cal.getTime(); cal.set(Calendar.HOUR_OF_DAY, Schedule.DEFAULT_END_HOUR); cal.set(Calendar.MINUTE, Schedule.DEFAULT_END_MINUTE); newSchedule.end = cal.getTime(); // ?? Intent intent = EditActivity.createIntent(CalendarActivity.this, mLoginUser, newSchedule); int requestCode = EditActivity.REQUEST_ADD_SCHEDULE; startActivityForResult(intent, requestCode); return true; } //????????? else if (id == android.R.id.home) { finish(); return true; } return super.onOptionsItemSelected(item); } @Override public boolean dispatchKeyEvent(KeyEvent event) { // Back if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { if (mDisplayedUser.usid.equals(mLoginUser.usid)) { if (!mBackKeyPressed) { // Timer //mBackKeyPressed.cancel(); // ??? mBackKeyTimer.start(); // ??, ????? mToast = Toast.makeText(CalendarActivity.this, getString(R.string.back_to_exit), Toast.LENGTH_SHORT); mToast.show(); mBackKeyPressed = true; return false; } } // pressed=true???Back??. return super.dispatchKeyEvent(event); } // Back???????????. return super.dispatchKeyEvent(event); } @Override protected void onDestroy() { // ?????? if (mToast != null) { mToast.cancel(); } super.onDestroy(); } }