Skip to content

Instantly share code, notes, and snippets.

@jaredrummler
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredrummler/1aae2464cfdbd99eeee8 to your computer and use it in GitHub Desktop.
Save jaredrummler/1aae2464cfdbd99eeee8 to your computer and use it in GitHub Desktop.
Get a Google Now Wallpaper by the time of day (dawn, day, dusk, night)
/*
* Copyright (C) 2014 Jared Rummler <jared@jrummyapps.com>
*
* 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.jrummyapps.wallpaper;
import java.util.Calendar;
import java.util.Random;
import android.util.Log;
/**
* Google Now Wallpaper from http://forum.xda-developers.com/showpost.php?p=37322378&postcount=98
*/
public class GoogleNowWalls {
/**
* Get a random google now wallpaper based on the time of day.
*
* @param dimensions The width and height of the image
* @return A random Google Now wallpaper URL based on the time of day.
*/
public static String getRandomGoogleNowWallpaperUrl(int...dimensions) {
return getRandomGoogleNowWallpaperUrl(DayInterval.now(), dimensions);
}
/**
*
* @param interval The width and height of the image
* @param dimensions
* @return A random Google Now wallpaper URL based on the time of day.
*/
public static String getRandomGoogleNowWallpaperUrl(DayInterval interval, int...dimensions) {
final Wallpaper[] wallpapers = Wallpaper.values();
return wallpapers[new Random().nextInt(wallpapers.length)].getUrl(interval, dimensions);
}
/**
* Used to break up a 24 hour day between dawn, day, dusk, and night.
*/
public enum DayInterval {
/** 5:00 am - 6:59 am */
Dawn(5, 6),
/** 7:00 am - 5:59 pm */
Day(7, 17),
/** 6:00 pm - 7:59 pm */
Dusk(18, 19),
/** 8:00 pm - 4:59 am */
Night(20, 4);
int[] hours;
DayInterval(int...hours) {
this.hours = hours;
}
/**
* Returns either {@link DayInterval#Dawn}, {@link DayInterval#Day},
* {@link DayInterval#Dusk} or {@link DayInterval#Night} depending on
* the hour of the day.</p>
*
* Ideally, you need latitude and months (and possibly any special rules for certain countries).</p>
*
* @return A {@link DayInterval} corresponding with the current hour of the day.
*/
public static DayInterval now() {
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR_OF_DAY);
DayInterval[] intervals = DayInterval.values();
for (DayInterval interval : intervals) {
if (interval.hours[1] < interval.hours[0]) {
// Night
if (hour <= interval.hours[1] || hour >= interval.hours[0]) {
return interval;
}
}
else if (hour >= interval.hours[0] && hour <= interval.hours[1]) {
return interval;
}
}
return DayInterval.Day;
}
}
/**
* Google Now Wallpapers
*/
public enum Wallpaper {
San_Francisco(0),
London(1),
Mountains(2),
Great_Plains(3),
Seattle(4),
New_York(5),
Chicago(6),
Honolulu(7),
Tahoe(8),
Berlin(9),
Austin(10);
int index;
Wallpaper(int index) {
this.index = index;
}
String[] getImageUrls(DayInterval interval) {
switch (interval) {
case Dawn:
return DAWN;
case Day:
return DAY;
case Dusk:
return DUSK;
case Night:
return NIGHT;
default:
return DAY;
}
}
public String getUrl(int...dimensions) {
return getUrl(DayInterval.now(), dimensions);
}
public String getUrl(DayInterval interval, int...dimensions) {
final String url = getWallpaperUrl(getImageUrls(interval), index, dimensions);
Log.i(Wallpaper.class.getSimpleName(), "Wallpaper: " + url + " | Location: " + toString());
return url;
}
}
private static final String[] DAWN = {
"http://lh4.ggpht.com/IBBjIVcMzPNui1BwBg2xK9VrA03RZ6l4PPr9nlMWJSe8lS1bI1q_0ObcIZ7aIsER=w%s-h%s", // San Francisco
"http://lh5.ggpht.com/_yM2g1jXS3GxmovyMTAo5-8q7mNY8CYZlh8rQK0xNeIfJMytLlf2FoIXM_F3wg=w%s-h%s", // London
"http://lh5.ggpht.com/LeDpxkfCDssG2jwo20Tg01UxnUc4-PZUojwKsPzIQoGJ_CgbXc7KVko8o3nk5zA=w%s-h%s", // Mountains
"http://lh3.ggpht.com/-eRaUK3T0bbuJdhKDMJ-wgwNmkvOgp1t7qvw-0-4Y27jFauvwyFyAPNxQCqrlgc=w%s-h%s", // Great Plains
"http://lh5.ggpht.com/x7wYMKpb2CGWK7Gqck0kSAX9uOHVvduWBresMNwtlokpVzOwQ9lOfEXsZgM75Gti=w%s-h%s", // Seattle
"http://lh6.ggpht.com/40TWudSUSlS-D-RNu0ZQRlOrifj7K3C-hKKwstSjYsNjvWGkVQ_-QPRU5b76XAI=w%s-h%s", // New York
"http://lh4.ggpht.com/HkTiPVXRlpbfe8FDJz6jlLW7kVNL6lJo-PaedbBq6HcagzCjLNt6lQ3OgDWRBI8=w%s-h%s", // Chicago
"http://lh4.ggpht.com/IjAj-JLbrX9CtwOXvR5R94zWVd_-De7yb-KSSCauKFjL7fc7u05lh4KxlABhT1g=w%s-h%s", // Honolulu
"http://lh6.ggpht.com/iM89ChxsvNmC7ajPUgbkZgHuuD6JB9xtUbmh5ov1hDZqSDDZNCpByENjjiv9WYq2=w%s-h%s", // Tahoe
"http://lh5.ggpht.com/8ubzNsDLQGQ8fYot0UcT0pWeqXPe_Z3jCpm398FuzE8ulYy5ysUXkVnUkwbFof8t=w%s-h%s", // Berlin
"http://lh4.ggpht.com/zAQwPdGWDc8a11nnSPM9peFsPHHzXxa0v1BGttsWfFy4g-kUXipz0zvcrXuGsKE=w%s-h%s" // Austin
};
private static final String[] DAY = {
"http://lh4.ggpht.com/eSJx22wFR4rSVcVI_yOVDmrrF696eptumrxJLlmWpL3T-riRuXoRvgBhesx8mg=w%s-h%s", // San Francisco
"http://lh6.ggpht.com/SdhQ1CORhZsvITVgd6MPRJ2Nx3U4JupHEs9qEPjImVkZf-sLB6hhDWIsGpALlvo=w%s-h%s", // London
"http://lh5.ggpht.com/bosDZkBJxNdwo-dXGZeBkYtfCVnTFq96zqC08UV4dmIccI4YBr5p0CyCE7vmj2w=w%s-h%s", // Mountains
"http://lh4.ggpht.com/OBlnWOdeWdlVR0fgVsbSWYuXVHudBGd382Yv6ckNNBPQbFIdWWJFMdch5H3a7Kw=w%s-h%s", // Great Plains
"http://lh4.ggpht.com/4kPg4fdvMJgnIgfFfCso_Ui8BHaVibW4VQb6vYq2WuUXJ7g_xU-qmUYJoZ1Y6w=w%s-h%s", // Seattle
"http://lh4.ggpht.com/OONnhdgrhh1x14gmAeHL6joB09j0Yn7ycEgSx2Dk75cEfZFVDeQp_hFHMkZxeWw=w%s-h%s", // New York
"http://lh4.ggpht.com/XKhGtSHgY06OVIHNVxMfvvcKhWGOI7qhLbSmOJ9p58GA-v7xCFvhHCyWIXm_dg=w%s-h%s", // Chicago
"http://lh5.ggpht.com/dhrhITD19-MNErPWdzr9s7gD3Dw6XdbqemQ9lFsXDDHwRgTBrRN2aAygtbjcWpY=w%s-h%s", // Honolulu
"http://lh5.ggpht.com/dhmfa5_k78ytD0v71S7EMyFN13MOQMzaLFwUVNCNsNXkjcRfgshLhyuBo3IRpQ=w%s-h%s", // Tahoe
"http://lh3.ggpht.com/q7AvSpLNCAOjlQAzPyh73cJhUewNo6-fznEvQxP07U7Z7IzuV53oZG5Lyh4Mkw=w%s-h%s", // Berlin
"http://lh4.ggpht.com/GB25hdlTR-h07vrrB5XRNyQMfPxpTmP9DRu7DW2iTjj4_B0ywcbulk4MX5tmeQ=w%s-h%s" // Austin
};
private static final String[] DUSK = {
"http://lh6.ggpht.com/G4Pl--INh2dHgjjYF6Kr_0Cc85SK9eP6UespNcBwimb1G0HmG9Y99T0OVL6lHTSd=w%s-h%s", // San Francisco
"http://lh6.ggpht.com/6OWsC7rvXQX8D4pzAlfOk9qaRvszP4vDuTERy42h2dMADmEHI26AYYDrUcgMyQ=w%s-h%s", // London
"http://lh4.ggpht.com/DCGfFj7ILzkFXXDgCliyTAq-cjKs8eyoTstREjhB2grAzzjYnlelGfpIQ4cEX4c=w%s-h%s", // Mountains
"http://lh5.ggpht.com/a7BiG1mNWojYk95fHBJCJKuJIarvQqvr6TaDTdTNY0DjQckdKq4MX3mJQkGI3LI=w%s-h%s", // Great Plains
"http://lh5.ggpht.com/_QetNT7lfWzeWXZArk0y-kq3ZQCps9l4PZK9G2UxHXLMiK_nDEKDGhmQvqbYBs0=w%s-h%s", // Seattle
"http://lh5.ggpht.com/qhVIITlUyAKh0l1fy1sIA32b0vnj-g5n07vhnhFqD2YovgXST5N4up4Mtb0Aa4Y=w%s-h%s", // New York
"http://lh3.ggpht.com/Fc15BNNdT8-aBORa8THhrf4dF6a-mslmwd3pXu0-sqvQrynDeD0i17K0eAX4a2E=w%s-h%s", // Chicago
"http://lh6.ggpht.com/E8ubHH8XziIK5t3ou9uj1xpq8XKNGKL-OIha6rnlhq1qCXPOvrI1eXqAn7z5A-A=w%s-h%s", // Honolulu
"http://lh5.ggpht.com/dswhgQX4GaEif9sy__jfVdf1vevCkdDHPmxV7Nu3JGu15aApntn3lYIkq_oY=w%s-h%s", // Tahoe
"http://lh6.ggpht.com/-30idiQtgOyr5KF7mYdV8cc85SNqLFcgBzT_UQCBKCQcGg7LhTfiATkNiToVYg=w%s-h%s", // Berlin
"http://lh5.ggpht.com/U7M2euecz7uMJg1KA_jhcJcCXg4ZQ7ZZmQIr2aUingqFm060K1FxZu2md6zfvF-T=w%s-h%s" // Austin
};
private static final String[] NIGHT = {
"http://lh5.ggpht.com/6rLbkZWhGVPxAVCosBf3AMhEtiie5jUl9qAfOS0HV6bExLpNOmnwezZnAlHwDA=w%s-h%s", // San Francisco
"http://lh3.ggpht.com/b95yYOipfo5QmknDxUhqyJz52pfncOYwhXhMpt7Yu3W3ycwZidLpWtC388SmRxB5=w%s-h%s", // London
"http://lh6.ggpht.com/QgqUFGYoAxRkyvbl_5Hq2L6CTsaGXt9kaqrMdSxga-462Uyv2IViGw7OBzDMWNI=w%s-h%s", // Mountains
"http://lh4.ggpht.com/BkE3y4c_8D3gTl2k6dLfmnKWha5e45XnKZP8NJ4pG4pXUb_J19_QBrxrthRk1A=w%s-h%s", // Great Plains
"http://lh3.ggpht.com/3ctptPGg5pMe2gjbqNpDX8ZzgrSz1HWa0YfyshLsom8XAvFQJJtsJZIuB_uqyQ=w%s-h%s", // Seattle
"http://lh6.ggpht.com/dq1rtn76Xertxh17J02UknLxRUgLX04f6dXjeTctgEnHudDHro_ViIJ1F68fyu3z=w%s-h%s", // New York
"http://lh6.ggpht.com/RlSDbCW2Fs5fHEzUXbVkAhjtzyaULSxG1zsUz0OmTgajiIL_cBMk4jLYnPxnB0cP=w%s-h%s", // Chicago
"http://lh4.ggpht.com/83jTpNKw0R4OW3hecAeo_9D56xY1c9o3a-opn8e8j0UG8lHy6vE8x2Ocum1GctE=w%s-h%s", // Honolulu
"http://lh4.ggpht.com/2oiPv-lr_VLFIHKa1KEKbzlVs1LGZukNfu0BWVzXPUZuG6bMfwTWtaVsvKFJkZUu=w%s-h%s", // Tahoe
"http://lh4.ggpht.com/2JEs07oznuuvgcgzgjAxRSskgqj3LI9gpATJuoYU58R4h5Yg4EmEfWy3QVQdXwQ=w%s-h%s", // Berlin
"http://lh5.ggpht.com/weQrxcZZp-Ut00kjauDe_cE4lbwtjwU__Xv-XtrXMBbw2lLwpb6dsevmW86MWw=w%s-h%s" // Austin
};
private static String getWallpaperUrl(String[] arr, int index, int...dimensions) {
String width, height;
if (dimensions != null && dimensions.length == 2) {
width = Integer.toString(dimensions[0]);
height = Integer.toString(dimensions[1]);
} else {
width = "9999";
height = "9999";
}
return String.format(arr[index], width, height);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment