Skip to content

Commit

Permalink
Fixed some stuff. Added location.
Browse files Browse the repository at this point in the history
  • Loading branch information
toffeegryphon committed Sep 20, 2019
1 parent b7405a1 commit e762adf
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

/**
* Instrumented test, which will execute on an Android device.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.service.wallpaper.WallpaperService;
import android.text.TextPaint;
import android.util.DisplayMetrics;
import android.util.JsonReader;
import android.util.Log;
import android.util.SparseArray;
import android.util.SparseIntArray;
Expand All @@ -28,16 +29,28 @@

import androidx.core.content.res.ResourcesCompat;

import org.json.JSONObject;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SubsolarWallpaperService extends WallpaperService {

Expand Down Expand Up @@ -96,15 +109,16 @@ private void draw() {
canvas = holder.lockCanvas();
if (canvas != null) {
Subsolar.Coordinate coordinate = Subsolar.equationOfTime();
DecimalFormat format = new DecimalFormat("###.00");

// new RetrieveBitmapTask(this).execute("https://api.mapbox.com/styles/v1/mapbox/satellite-v9/static/" + coordinate.longitude + "," + coordinate.latitude + ",8,0/" + metrics.x + "x" + 1280 + "?access_token=pk.eyJ1IjoidG9mZmVlZ3J5cGhvbiIsImEiOiJjazBxajF1Mm4wOGRoM21tc2UyNmRlYWo0In0.r5yVFHioIJHF4FYdXypWNA");
// if (bitmap != null) {
// canvas.drawBitmap(bitmap, null, new Rect(0, 0, metrics.x, 1280), null);
// }

//TODO double confirm if I got the right tile
double x = (coordinate.longitude + 180.0) / 2.8125 + 1.0;
double y = (coordinate.latitude + 85.0511) / 1.3289234375 + 1.0;
double x = (coordinate.longitude + 180.0) / 2.8125 - 1.0;
double y = (coordinate.latitude + 85.0511) / 1.3289234375 - 1.0;
Point tile = new Point((int) x, (int) y);
double dX = x - tile.x;
double dY = y - tile.y;
Expand All @@ -121,6 +135,8 @@ private void draw() {

pY = pY - (tile.y - firstY) * bitmapLength;

// new RetrieveLocationAsyncTask().execute(new Subsolar.Coordinate(Double.valueOf(format.format(coordinate.latitude)), Double.valueOf(format.format(coordinate.longitude))));

Paint refresh = new Paint();
refresh.setColor(Color.BLACK);
refresh.setStyle(Paint.Style.FILL);
Expand Down Expand Up @@ -192,9 +208,10 @@ private void draw() {
west -= 1;
}

text.setTextSize(100);
text.setTextSize(60);
refresh.setColor(Color.YELLOW);
canvas.drawCircle(metrics.x / 2, metrics.y / 2, 50, refresh);
refresh.setAlpha(150);
canvas.drawCircle(metrics.x / 2, metrics.y / 2, 30, refresh);

//TODO allow user to add image as their sun haha so that their SO can be their sun

Expand All @@ -205,17 +222,31 @@ private void draw() {
Log.d("ADDRESSES", addresses.toString());
String city = addresses.get(0).getLocality();
String country = addresses.get(0).getCountryName();
canvas.drawText(String.format("%s, %s", city, country), metrics.x / 16, metrics.y * 5/8, text);
if (country.length() >= 15) {
Pattern pattern = Pattern.compile("[A-Z\\s]+");
Matcher matcher = pattern.matcher(country);
StringBuilder res = new StringBuilder();
while (matcher.find()) {
res.append(matcher.group());
}
country = res.toString();
}
String locale;
if (city == null) {
locale = country;
} else {
locale = String.format("%s, %s", city, country);
}
canvas.drawText(locale, metrics.x / 2 - 150, metrics.y /2, text);
} catch (IOException e) {
e.printStackTrace();
canvas.drawText("Ocean", metrics.x / 8, metrics.y / 2, text);
// canvas.drawText("Ocean", metrics.x / 2 - 200, metrics.y * 5/8, text);
}

// Draw Lat, Long
text.setTypeface(dense);
text.setTextSize(40);
DecimalFormat format = new DecimalFormat("###.00");
canvas.drawText(String.format("%s, %s", format.format(coordinate.latitude), format.format(coordinate.longitude)), metrics.x / 8, metrics.y / 2 + 40, text);
canvas.drawText(String.format("%s, %s", format.format(coordinate.latitude), format.format(coordinate.longitude)), metrics.x / 2 - 150, metrics.y / 2 + 40, text);
}
} finally {
if (canvas != null) {
Expand Down Expand Up @@ -289,6 +320,34 @@ protected void onPostExecute(Bitmap bitmap) {
}
}

static class RetrieveLocationAsyncTask extends AsyncTask<Subsolar.Coordinate, Void, ArrayList<String>> {

@Override
protected ArrayList<String> doInBackground(Subsolar.Coordinate... coordinates) {
ArrayList<String> result = new ArrayList<>();
try {
URL url = new URL(String.format("https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=%s&lon=%s&zoom=10&addressdetails=1", coordinates[0].latitude, coordinates[0].longitude));
Log.d("URL", String.valueOf(url));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
JsonReader reader = new JsonReader(new InputStreamReader(input, StandardCharsets.UTF_8));
while (reader.hasNext()) {
Log.d("JSON", reader.nextName());
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}

@Override
protected void onPostExecute(ArrayList<String> result) {

}
}

interface OnRetrievedBitmapListener {
void onBitmapRetrieved(Bitmap bitmap);
}
Expand Down

0 comments on commit e762adf

Please sign in to comment.