List of usage examples for android.text.format Time getJulianDay
public static int getJulianDay(long millis, long gmtoff)
From source file:com.android.calendar.month.MonthByWeekFragment.java
@Override public void handleEvent(EventInfo event) { if (event.eventType == EventType.GO_TO) { boolean animate = true; if (mDaysPerWeek * mNumWeeks * 2 < Math.abs(Time.getJulianDay(event.selectedTime.toMillis(true), event.selectedTime.gmtoff) - Time.getJulianDay(mFirstVisibleDay.toMillis(true), mFirstVisibleDay.gmtoff) - mDaysPerWeek * mNumWeeks / 2)) { animate = false;//from w w w .j av a 2 s . c o m } mDesiredDay.set(event.selectedTime); mDesiredDay.normalize(true); boolean animateToday = (event.extraLong & CalendarController.EXTRA_GOTO_TODAY) != 0; boolean delayAnimation = goTo(event.selectedTime.toMillis(true), animate, true, false); if (animateToday) { // If we need to flash today start the animation after any // movement from listView has ended. mHandler.postDelayed(new Runnable() { @Override public void run() { ((MonthByWeekAdapter) mAdapter).animateToday(); mAdapter.notifyDataSetChanged(); } }, delayAnimation ? GOTO_SCROLL_DURATION : 0); } } else if (event.eventType == EventType.EVENTS_CHANGED) { eventsChanged(); } }
From source file:com.katamaditya.apps.weather4u.weathersync.Weather4USyncAdapter.java
/** * Take the String representing the complete forecast in JSON Format and * pull out the data we need to construct the Strings needed for the wireframes. * <p/>/*from w w w.j av a 2 s. co m*/ * Fortunately parsing is easy: constructor takes the JSON string and converts it * into an Object hierarchy for us. */ private void getWeatherDataFromJson(String forecastJsonStr, String locationSetting) throws JSONException { // Now we have a String representing the complete forecast in JSON Format. // Fortunately parsing is easy: constructor takes the JSON string and converts it // into an Object hierarchy for us. // These are the names of the JSON objects that need to be extracted. // Location information final String OWM_CITY = "city"; final String OWM_CITY_NAME = "name"; final String OWM_COORD = "coord"; // Location coordinate final String OWM_LATITUDE = "lat"; final String OWM_LONGITUDE = "lon"; // Weather information. Each day's forecast info is an element of the "list" array. final String OWM_LIST = "list"; final String OWM_PRESSURE = "pressure"; final String OWM_HUMIDITY = "humidity"; final String OWM_WINDSPEED = "speed"; final String OWM_WIND_DIRECTION = "deg"; // All temperatures are children of the "temp" object. final String OWM_TEMPERATURE = "temp"; final String OWM_MAX = "max"; final String OWM_MIN = "min"; final String OWM_WEATHER = "weather"; final String OWM_DESCRIPTION = "main"; final String OWM_WEATHER_ID = "id"; try { JSONObject forecastJson = new JSONObject(forecastJsonStr); JSONArray weatherArray = forecastJson.getJSONArray(OWM_LIST); JSONObject cityJson = forecastJson.getJSONObject(OWM_CITY); String cityName = cityJson.getString(OWM_CITY_NAME); JSONObject cityCoord = cityJson.getJSONObject(OWM_COORD); double cityLatitude = cityCoord.getDouble(OWM_LATITUDE); double cityLongitude = cityCoord.getDouble(OWM_LONGITUDE); long locationId = addLocation(locationSetting, cityName, cityLatitude, cityLongitude); // Insert the new weather information into the database Vector<ContentValues> cVVector = new Vector<ContentValues>(weatherArray.length()); // OWM returns daily forecasts based upon the local time of the city that is being // asked for, which means that we need to know the GMT offset to translate this data // properly. // Since this data is also sent in-order and the first day is always the // current day, we're going to take advantage of that to get a nice // normalized UTC date for all of our weather. Time dayTime = new Time(); dayTime.setToNow(); // we start at the day returned by local time. Otherwise this is a mess. int julianStartDay = Time.getJulianDay(System.currentTimeMillis(), dayTime.gmtoff); // now we work exclusively in UTC dayTime = new Time(); for (int i = 0; i < weatherArray.length(); i++) { // These are the values that will be collected. long dateTime; double pressure; int humidity; double windSpeed; double windDirection; double high; double low; String description; int weatherId; // Get the JSON object representing the day JSONObject dayForecast = weatherArray.getJSONObject(i); // Cheating to convert this to UTC time, which is what we want anyhow dateTime = dayTime.setJulianDay(julianStartDay + i); pressure = dayForecast.getDouble(OWM_PRESSURE); humidity = dayForecast.getInt(OWM_HUMIDITY); windSpeed = dayForecast.getDouble(OWM_WINDSPEED); windDirection = dayForecast.getDouble(OWM_WIND_DIRECTION); // Description is in a child array called "weather", which is 1 element long. // That element also contains a weather code. JSONObject weatherObject = dayForecast.getJSONArray(OWM_WEATHER).getJSONObject(0); description = weatherObject.getString(OWM_DESCRIPTION); weatherId = weatherObject.getInt(OWM_WEATHER_ID); // Temperatures are in a child object called "temp". Try not to name variables // "temp" when working with temperature. It confuses everybody. JSONObject temperatureObject = dayForecast.getJSONObject(OWM_TEMPERATURE); high = temperatureObject.getDouble(OWM_MAX); low = temperatureObject.getDouble(OWM_MIN); ContentValues weatherValues = new ContentValues(); weatherValues.put(WeatherContract.WeatherEntry.COLUMN_LOC_KEY, locationId); weatherValues.put(WeatherContract.WeatherEntry.COLUMN_DATE, dateTime); weatherValues.put(WeatherContract.WeatherEntry.COLUMN_HUMIDITY, humidity); weatherValues.put(WeatherContract.WeatherEntry.COLUMN_PRESSURE, pressure); weatherValues.put(WeatherContract.WeatherEntry.COLUMN_WIND_SPEED, windSpeed); weatherValues.put(WeatherContract.WeatherEntry.COLUMN_DEGREES, windDirection); weatherValues.put(WeatherContract.WeatherEntry.COLUMN_MAX_TEMP, high); weatherValues.put(WeatherContract.WeatherEntry.COLUMN_MIN_TEMP, low); weatherValues.put(WeatherContract.WeatherEntry.COLUMN_SHORT_DESC, description); weatherValues.put(WeatherContract.WeatherEntry.COLUMN_WEATHER_ID, weatherId); cVVector.add(weatherValues); } int inserted = 0; // add to database if (cVVector.size() > 0) { ContentValues[] cvArray = new ContentValues[cVVector.size()]; cVVector.toArray(cvArray); getContext().getContentResolver().bulkInsert(WeatherContract.WeatherEntry.CONTENT_URI, cvArray); // delete old data so we don't build up an endless history getContext().getContentResolver().delete(WeatherContract.WeatherEntry.CONTENT_URI, WeatherContract.WeatherEntry.COLUMN_DATE + " <= ?", new String[] { Long.toString(dayTime.setJulianDay(julianStartDay - 1)) }); notifyWeather(); } } catch (JSONException e) { //Log.e(LOG_TAG, e.getMessage(), e); e.printStackTrace(); } }
From source file:com.jackie.sunshine.app.sync.SunshineSyncAdapter.java
/** * Take the String representing the complete forecast in JSON Format and * pull out the data we need to construct the Strings needed for the wireframes. * <p>// w w w .j a v a2 s. c om * Fortunately parsing is easy: constructor takes the JSON string and converts it * into an Object hierarchy for us. */ private void getWeatherDataFromJson(String forecastJsonStr, String locationSetting) throws JSONException { // Now we have a String representing the complete forecast in JSON Format. // Fortunately parsing is easy: constructor takes the JSON string and converts it // into an Object hierarchy for us. // These are the names of the JSON objects that need to be extracted. // Location information final String OWM_CITY = "city"; final String OWM_CITY_NAME = "name"; final String OWM_COORD = "coord"; // Location coordinate final String OWM_LATITUDE = "lat"; final String OWM_LONGITUDE = "lon"; // Weather information. Each day's forecast info is an element of the "list" array. final String OWM_LIST = "list"; final String OWM_PRESSURE = "pressure"; final String OWM_HUMIDITY = "humidity"; final String OWM_WINDSPEED = "speed"; final String OWM_WIND_DIRECTION = "deg"; // All temperatures are children of the "temp" object. final String OWM_TEMPERATURE = "temp"; final String OWM_MAX = "max"; final String OWM_MIN = "min"; final String OWM_WEATHER = "weather"; final String OWM_DESCRIPTION = "main"; final String OWM_WEATHER_ID = "id"; try { JSONObject forecastJson = new JSONObject(forecastJsonStr); JSONArray weatherArray = forecastJson.getJSONArray(OWM_LIST); JSONObject cityJson = forecastJson.getJSONObject(OWM_CITY); String cityName = cityJson.getString(OWM_CITY_NAME); JSONObject cityCoord = cityJson.getJSONObject(OWM_COORD); double cityLatitude = cityCoord.getDouble(OWM_LATITUDE); double cityLongitude = cityCoord.getDouble(OWM_LONGITUDE); long locationId = addLocation(locationSetting, cityName, cityLatitude, cityLongitude); // Insert the new weather information into the database Vector<ContentValues> cVVector = new Vector<>(weatherArray.length()); // OWM returns daily forecasts based upon the local time of the city that is being // asked for, which means that we need to know the GMT offset to translate this data // properly. // Since this data is also sent in-order and the first day is always the // current day, we're going to take advantage of that to get a nice // normalized UTC date for all of our weather. GregorianCalendar calendar = new GregorianCalendar(); Time dayTime = new Time(); dayTime.setToNow(); // we start at the day returned by local time. Otherwise this is a mess. int julianStartDay = Time.getJulianDay(System.currentTimeMillis(), dayTime.gmtoff); // now we work exclusively in UTC dayTime = new Time(); for (int i = 0; i < weatherArray.length(); i++) { // These are the values that will be collected. long dateTime; double pressure; int humidity; double windSpeed; double windDirection; double high; double low; String description; int weatherId; // Get the JSON object representing the day JSONObject dayForecast = weatherArray.getJSONObject(i); // Cheating to convert this to UTC time, which is what we want anyhow dateTime = dayTime.setJulianDay(julianStartDay + i); pressure = dayForecast.getDouble(OWM_PRESSURE); humidity = dayForecast.getInt(OWM_HUMIDITY); windSpeed = dayForecast.getDouble(OWM_WINDSPEED); windDirection = dayForecast.getDouble(OWM_WIND_DIRECTION); // Description is in a child array called "weather", which is 1 element long. // That element also contains a weather code. JSONObject weatherObject = dayForecast.getJSONArray(OWM_WEATHER).getJSONObject(0); description = weatherObject.getString(OWM_DESCRIPTION); weatherId = weatherObject.getInt(OWM_WEATHER_ID); // Temperatures are in a child object called "temp". Try not to name variables // "temp" when working with temperature. It confuses everybody. JSONObject temperatureObject = dayForecast.getJSONObject(OWM_TEMPERATURE); high = temperatureObject.getDouble(OWM_MAX); low = temperatureObject.getDouble(OWM_MIN); ContentValues weatherValues = new ContentValues(); weatherValues.put(WeatherEntry.COLUMN_LOC_KEY, locationId); weatherValues.put(WeatherEntry.COLUMN_DATE, dateTime); weatherValues.put(WeatherEntry.COLUMN_HUMIDITY, humidity); weatherValues.put(WeatherEntry.COLUMN_PRESSURE, pressure); weatherValues.put(WeatherEntry.COLUMN_WIND_SPEED, windSpeed); weatherValues.put(WeatherEntry.COLUMN_DEGREES, windDirection); weatherValues.put(WeatherEntry.COLUMN_MAX_TEMP, high); weatherValues.put(WeatherEntry.COLUMN_MIN_TEMP, low); weatherValues.put(WeatherEntry.COLUMN_SHORT_DESC, description); weatherValues.put(WeatherEntry.COLUMN_WEATHER_ID, weatherId); cVVector.add(weatherValues); } int inserted = 0; // add to database if (cVVector.size() > 0) { // Student: call bulkInsert to add the weatherEntries to the database here ContentValues[] cvArray = new ContentValues[cVVector.size()]; cVVector.toArray(cvArray); ContentResolver contentResolver = getContext().getContentResolver(); inserted = contentResolver.bulkInsert(WeatherEntry.CONTENT_URI, cvArray); contentResolver.delete(WeatherEntry.CONTENT_URI, WeatherEntry.COLUMN_DATE + "<= " + "?", new String[] { Long.toString(dayTime.setJulianDay(julianStartDay - 1)) }); notifyWeather(); } Log.d(TAG, "getWeatherDataFromJson: " + inserted + " inserted"); } catch (JSONException e) { Log.e(TAG, e.getMessage(), e); e.printStackTrace(); } }
From source file:de.tum.in.tumcampus.auxiliary.calendar.DayView.java
private void init(Context context) { setFocusable(true);/*w ww. j a va 2 s. c om*/ // Allow focus in touch mode so that we can do keyboard shortcuts // even after we've entered touch mode. setFocusableInTouchMode(true); setClickable(true); setOnCreateContextMenuListener(this); mFirstDayOfWeek = Time.MONDAY; mCurrentTime = new Time(DayUtils.getTimeZone(context, mTZUpdater)); long currentTime = System.currentTimeMillis(); mCurrentTime.set(currentTime); mTodayJulianDay = Time.getJulianDay(currentTime, mCurrentTime.gmtoff); mEventTextPaint.setTextSize(EVENT_TEXT_FONT_SIZE); mEventTextPaint.setTextAlign(Align.LEFT); mEventTextPaint.setAntiAlias(true); int gridLineColor = 0xff707070; Paint p = mSelectionPaint; p.setColor(gridLineColor); p.setStyle(Style.FILL); p.setAntiAlias(false); p = mPaint; p.setAntiAlias(true); // Long day names mDayStrsLong = new String[14]; // Allocate space for 2 weeks worth of weekday names so that we can // easily start the week display at any week day. mDayStrs = new String[14]; // Also create an array of 2-letter abbreviations. mDayStrs2Letter = new String[14]; for (int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++) { int index = i - Calendar.SUNDAY; mDayStrsLong[index] = DateUtils.getDayOfWeekString(i, DateUtils.LENGTH_LONG).toUpperCase(); mDayStrsLong[index + 7] = mDayStrsLong[index]; // e.g. Tue for Tuesday mDayStrs[index] = DateUtils.getDayOfWeekString(i, DateUtils.LENGTH_MEDIUM).toUpperCase(); mDayStrs[index + 7] = mDayStrs[index]; // e.g. Tu for Tuesday mDayStrs2Letter[index] = DateUtils.getDayOfWeekString(i, DateUtils.LENGTH_SHORT).toUpperCase(); // If we don't have 2-letter day strings, fall back to 1-letter. if (mDayStrs2Letter[index].equals(mDayStrs[index])) { mDayStrs2Letter[index] = DateUtils.getDayOfWeekString(i, DateUtils.LENGTH_SHORTEST); } mDayStrs2Letter[index + 7] = mDayStrs2Letter[index]; } // Figure out how much space we need for the 3-letter abbrev names // in the worst case. p.setTextSize(DATE_HEADER_FONT_SIZE); p.setTypeface(mBold); String[] dateStrs = { " 28", " 30" }; mDateStrWidth = computeMaxStringWidth(0, dateStrs, p); Time time = new Time(); time.setJulianDay(mFirstJulianDay); String s = SimpleDateFormat.getDateInstance().format(new Date(time.toMillis(false))); mDateStrWidthLong = computeMaxStringWidth(0, new String[] { s }, p); p.setTextSize(DAY_HEADER_FONT_SIZE); mDateStrWidth += computeMaxStringWidth(0, mDayStrs, p); mDateStrWidthLong += computeMaxStringWidth(0, mDayStrsLong, p); p.setTextSize(HOURS_TEXT_SIZE); p.setTypeface(null); handleOnResume(); mAmString = DateUtils.getAMPMString(Calendar.AM).toUpperCase(); mPmString = DateUtils.getAMPMString(Calendar.PM).toUpperCase(); String[] ampm = { mAmString, mPmString }; p.setTextSize(AMPM_TEXT_SIZE); mHoursWidth = Math.max(HOURS_MARGIN, computeMaxStringWidth(mHoursWidth, ampm, p) + HOURS_RIGHT_MARGIN); mHoursWidth = Math.max(MIN_HOURS_WIDTH, mHoursWidth); LayoutInflater inflater; inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); Resources.Theme dialogTheme = getResources().newTheme(); dialogTheme.applyStyle(android.R.style.Theme_Dialog, true); TypedArray ta = dialogTheme.obtainStyledAttributes(new int[] { android.R.attr.windowBackground }); // Catch long clicks for creating a new event mBaseDate = new Time(DayUtils.getTimeZone(context, mTZUpdater)); long millis = System.currentTimeMillis(); mBaseDate.set(millis); mEarliestStartHour = new int[mNumDays]; mHasAllDayEvent = new boolean[mNumDays]; // mLines is the array of points used with Canvas.drawLines() in // drawGridBackground() and drawAllDayEvents(). Its size depends // on the max number of lines that can ever be drawn by any single // drawLines() call in either of those methods. final int maxGridLines = (24 + 1) // max horizontal lines we might draw + (mNumDays + 1); // max vertical lines we might draw mLines = new float[maxGridLines * 4]; }
From source file:de.tum.in.tumcampus.auxiliary.calendar.DayView.java
public void setSelected(Time time, boolean ignoreTime, boolean animateToday) { mBaseDate.set(time);/*from www .j av a 2 s.c o m*/ setSelectedHour(mBaseDate.hour); setSelectedEvent(null); long millis = mBaseDate.toMillis(false /* use isDst */); setSelectedDay(Time.getJulianDay(millis, mBaseDate.gmtoff)); mSelectedEvents.clear(); mComputeSelectedEvents = true; int gotoY = Integer.MIN_VALUE; if (!ignoreTime && mGridAreaHeight != -1) { int lastHour = 0; if (mBaseDate.hour < mFirstHour) { // Above visible region gotoY = mBaseDate.hour * (mCellHeight + HOUR_GAP); } else { lastHour = (mGridAreaHeight - mFirstHourOffset) / (mCellHeight + HOUR_GAP) + mFirstHour; if (mBaseDate.hour >= lastHour) { // Below visible region // target hour + 1 (to give it room to see the event) - // grid height (to get the y of the top of the visible // region) gotoY = (int) ((mBaseDate.hour + 1 + mBaseDate.minute / 60.0f) * (mCellHeight + HOUR_GAP) - mGridAreaHeight); } } if (gotoY > mMaxViewStartY) { gotoY = mMaxViewStartY; } else if (gotoY < 0 && gotoY != Integer.MIN_VALUE) { gotoY = 0; } } recalc(); mRemeasure = true; invalidate(); boolean delayAnimateToday = false; if (gotoY != Integer.MIN_VALUE) { ValueAnimator scrollAnim = ObjectAnimator.ofInt(this, "viewStartY", mViewStartY, gotoY); scrollAnim.setDuration(GOTO_SCROLL_DURATION); scrollAnim.setInterpolator(new AccelerateDecelerateInterpolator()); scrollAnim.addListener(mAnimatorListener); scrollAnim.start(); delayAnimateToday = true; } if (animateToday) { synchronized (mTodayAnimatorListener) { if (mTodayAnimator != null) { mTodayAnimator.removeAllListeners(); mTodayAnimator.cancel(); } mTodayAnimator = ObjectAnimator.ofInt(this, "animateTodayAlpha", mAnimateTodayAlpha, 255); mAnimateToday = true; mTodayAnimatorListener.setFadingIn(true); mTodayAnimatorListener.setAnimator(mTodayAnimator); mTodayAnimator.addListener(mTodayAnimatorListener); mTodayAnimator.setDuration(150); if (delayAnimateToday) { mTodayAnimator.setStartDelay(GOTO_SCROLL_DURATION); } mTodayAnimator.start(); } } }
From source file:de.tum.in.tumcampus.auxiliary.calendar.DayView.java
private void recalc() { // Set the base date to the beginning of the week if we are displaying // 7 days at a time. if (mNumDays == 7) { adjustToBeginningOfWeek(mBaseDate); }//from w w w . j a v a 2 s. c o m final long start = mBaseDate.toMillis(false /* use isDst */); mFirstJulianDay = Time.getJulianDay(start, mBaseDate.gmtoff); mLastJulianDay = mFirstJulianDay + mNumDays - 1; mMonthLength = mBaseDate.getActualMaximum(Time.MONTH_DAY); mFirstVisibleDate = mBaseDate.monthDay; mFirstVisibleDayOfWeek = mBaseDate.weekDay; }
From source file:com.android.calendar.AllInOneActivity.java
private void updateSecondaryTitleFields(long visibleMillisSinceEpoch) { mShowWeekNum = Utils.getShowWeekNumber(this); mTimeZone = Utils.getTimeZone(this, mHomeTimeUpdater); if (visibleMillisSinceEpoch != -1) { int weekNum = Utils.getWeekNumberFromTime(visibleMillisSinceEpoch, this); mWeekNum = weekNum;/*w w w .j a v a2 s. c o m*/ } if (mShowWeekNum && (mCurrentView == ViewType.WEEK) && mIsTabletConfig && mWeekTextView != null) { String weekString = getResources().getQuantityString(R.plurals.weekN, mWeekNum, mWeekNum); mWeekTextView.setText(weekString); mWeekTextView.setVisibility(View.VISIBLE); } else if (visibleMillisSinceEpoch != -1 && mWeekTextView != null && mCurrentView == ViewType.DAY && mIsTabletConfig) { Time time = new Time(mTimeZone); time.set(visibleMillisSinceEpoch); int julianDay = Time.getJulianDay(visibleMillisSinceEpoch, time.gmtoff); time.setToNow(); int todayJulianDay = Time.getJulianDay(time.toMillis(false), time.gmtoff); String dayString = Utils.getDayOfWeekString(julianDay, todayJulianDay, visibleMillisSinceEpoch, this); mWeekTextView.setText(dayString); mWeekTextView.setVisibility(View.VISIBLE); } else if (mWeekTextView != null && (!mIsTabletConfig || mCurrentView != ViewType.DAY)) { mWeekTextView.setVisibility(View.GONE); } if (mHomeTime != null && (mCurrentView == ViewType.DAY || mCurrentView == ViewType.WEEK || mCurrentView == ViewType.AGENDA) && !TextUtils.equals(mTimeZone, Time.getCurrentTimezone())) { Time time = new Time(mTimeZone); time.setToNow(); long millis = time.toMillis(true); boolean isDST = time.isDst != 0; int flags = DateUtils.FORMAT_SHOW_TIME; if (DateFormat.is24HourFormat(this)) { flags |= DateUtils.FORMAT_24HOUR; } // Formats the time as String timeString = (new StringBuilder(Utils.formatDateRange(this, millis, millis, flags))).append(" ") .append(TimeZone.getTimeZone(mTimeZone).getDisplayName(isDST, TimeZone.SHORT, Locale.getDefault())) .toString(); mHomeTime.setText(timeString); mHomeTime.setVisibility(View.VISIBLE); // Update when the minute changes mHomeTime.removeCallbacks(mHomeTimeUpdater); mHomeTime.postDelayed(mHomeTimeUpdater, DateUtils.MINUTE_IN_MILLIS - (millis % DateUtils.MINUTE_IN_MILLIS)); } else if (mHomeTime != null) { mHomeTime.setVisibility(View.GONE); } }