Widget class revamped

- new layout
 - with new data
 - Services class amended
 - removal of all legacy code
This commit is contained in:
2020-11-28 02:40:21 +00:00
parent 7719d76f3f
commit 4db91cd3c1
46 changed files with 430 additions and 3644 deletions

Binary file not shown.

2
.idea/modules.xml generated
View File

@@ -4,6 +4,8 @@
<modules>
<module fileurl="file://$PROJECT_DIR$/Altas_-_Weather.iml" filepath="$PROJECT_DIR$/Altas_-_Weather.iml" group="Altas_-_Weather" />
<module fileurl="file://$PROJECT_DIR$/app/Altas_-_Weather-app.iml" filepath="$PROJECT_DIR$/app/Altas_-_Weather-app.iml" group="Altas_-_Weather/app" />
<module fileurl="file://$PROJECT_DIR$/.idea/modules/Android Studio Projects-Altas_-_Weather.iml" filepath="$PROJECT_DIR$/.idea/modules/Android Studio Projects-Altas_-_Weather.iml" group="Altas_-_Weather" />
<module fileurl="file://$PROJECT_DIR$/.idea/modules/app/Android Studio Projects-Altas_-_Weather-app.iml" filepath="$PROJECT_DIR$/.idea/modules/app/Android Studio Projects-Altas_-_Weather-app.iml" group="Altas_-_Weather/app" />
<module fileurl="file://$PROJECT_DIR$/Weather_app.iml" filepath="$PROJECT_DIR$/Weather_app.iml" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
</modules>

View File

@@ -34,39 +34,12 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- <activity-->
<!-- android:name=".legacy.ui.home.MainActivity"-->
<!-- android:label="@string/app_name"-->
<!-- android:launchMode="singleTop"-->
<!-- android:theme="@style/AppTheme.NoActionBar">-->
<!-- <intent-filter>-->
<!-- <action android:name="android.intent.action.MAIN" />-->
<!-- <action android:name="android.intent.action.VIEW" />-->
<!-- <category android:name="android.intent.category.LAUNCHER" />-->
<!-- </intent-filter>-->
<!-- </activity>-->
<receiver
android:name=".legacy.services.notifcation.NotificationReceiver"
android:name=".notifcation.NotificationReceiver"
android:parentActivityName="com.appttude.h_mal.atlas_weather.MainActivity" />
<activity
android:name=".legacy.ui.FurtherInfoActivity"
android:parentActivityName=".legacy.ui.home.MainActivity" />
<activity
android:name=".legacy.ui.AddForecastActivity"
android:parentActivityName=".legacy.ui.home.MainActivity" />
<activity
android:name=".legacy.ui.UnitSettingsActivity"
android:parentActivityName=".legacy.ui.home.MainActivity" />
<provider
android:name=".legacy.data.sql.ForecastProvider"
android:authorities="com.appttude.h_mal.atlas_weather"
android:exported="false" />
<receiver android:name=".legacy.AppWidget.NewAppWidget">
<receiver android:name=".widget.NewAppWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
@@ -78,17 +51,10 @@
android:resource="@xml/new_app_widget_info" />
</receiver>
<activity
android:name=".legacy.ui.WorldItemActivity"
android:parentActivityName=".legacy.ui.home.MainActivity"></activity>
<service
android:name=".legacy.AppWidget.WidgetRemoteViewsService"
android:name=".widget.WidgetRemoteViewsService"
android:permission="android.permission.BIND_REMOTEVIEWS" />
<activity
android:name=".legacy.ui.InfoActivity"
android:parentActivityName=".legacy.ui.home.MainActivity"></activity>
</application>
</manifest>

View File

@@ -1,169 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.AppWidget;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.AdapterView;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;
import com.appttude.h_mal.atlas_weather.R;
import com.appttude.h_mal.atlas_weather.legacy.data.sql.ForecastDBHelper;
import com.appttude.h_mal.atlas_weather.legacy.model.Forecast;
import com.appttude.h_mal.atlas_weather.legacy.model.ForecastItem;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import static com.appttude.h_mal.atlas_weather.legacy.data.network.RetrieveJSON.extractFeatureFromJson;
import static com.appttude.h_mal.atlas_weather.legacy.data.sql.ForecastContract.ForecastEntry.COLUMN_FORECAST_NAME;
import static com.appttude.h_mal.atlas_weather.legacy.data.sql.ForecastContract.ForecastEntry.COLUMN_WIDGET_FORECAST_ITEM;
import static com.appttude.h_mal.atlas_weather.legacy.data.sql.ForecastContract.ForecastEntry.TABLE_NAME_WIDGET;
import static com.appttude.h_mal.atlas_weather.legacy.ui.home.MainActivity.getImageResource;
public class MyWidgetRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
private Context mContext;
// private ForecastItem forecastItem;
private List<Forecast> forecastList;
private String TAG = this.getClass().getSimpleName();
private Cursor cursor;
private SQLiteDatabase database;
private int appWidgetId;
private SharedPreferences mSettings;
public MyWidgetRemoteViewsFactory(Context context, Intent intent) {
this.mContext = context;
this.appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
mSettings = PreferenceManager.getDefaultSharedPreferences(context);
}
@Override
public void onCreate() {
Log.i(TAG, "onCreate: widget oncreate executed");
ForecastDBHelper forecastsDbhelper = new ForecastDBHelper(mContext);
database = forecastsDbhelper.getWritableDatabase();
}
@Override
public void onDataSetChanged() {
if (cursor != null){
cursor.close();
}
String[] projection = {COLUMN_FORECAST_NAME, COLUMN_WIDGET_FORECAST_ITEM};
cursor = database.query(TABLE_NAME_WIDGET,
projection,
COLUMN_FORECAST_NAME + " IS ?",new String[]{"Current"},null,null,null);
String json;
if (cursor != null && cursor.getCount() > 0) {
Log.i(TAG, "onDataSetChanged: cursor loaded, count: " + cursor.getCount());
cursor.moveToFirst();
json = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_WIDGET_FORECAST_ITEM));
ForecastItem forecastItem = extractFeatureFromJson(json);
if (forecastItem != null) {
forecastList = forecastItem.getForecastArrayList();
forecastList.remove(0);
}
cursor.close();
}else{
Log.i(TAG, "onDataSetChanged: cursor is null");
}
}
@Override
public void onDestroy() {
}
@Override
public int getCount() {
Log.i(TAG, "getCount: size = " + forecastList.size());
return forecastList.size();
}
@Override
public RemoteViews getViewAt(int i) {
if (i == AdapterView.INVALID_POSITION ||
forecastList == null || forecastList.get(i) == null ) {
Log.i(this.getClass().getSimpleName(), "getViewAt: no views" );
return null;
}
Log.i(this.getClass().getSimpleName(), "getViewAt: views exist " + i);
RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
Date updatedate = new Date(forecastList.get(i).getDate_epoch()*1000);
SimpleDateFormat format = new SimpleDateFormat("EEE");
String dateText = format.format(updatedate);
// if (dateText.equals("Wednesday")){
// dateText = "Wednes..";
// }
//
// Log.i(TAG, "getViewAt: dateText: " + dateText);
//
// if(i == 0){
// SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
// String result = sdf.format(Calendar.getInstance().getTime());
// rv.setTextViewText(R.id.widget_item_day, result);
// }else {
rv.setTextViewText(R.id.widget_item_day, dateText);
// }
rv.setImageViewResource(R.id.widget_item_image,
getImageResource(forecastList.get(i).getIconURL(),mContext));
String maxtemp;
String mintemp;
if (mSettings.getString("temp_units","").equals("")){
maxtemp = String.valueOf(Math.round(forecastList.get(i).getMaxtemp_f())) + "°";
mintemp = String.valueOf(Math.round(forecastList.get(i).getMintemp_f())) + "°";
}else{
maxtemp = String.valueOf(Math.round(forecastList.get(i).getMaxtemp_c())) + "°";
mintemp = String.valueOf(Math.round(forecastList.get(i).getMintemp_c())) + "°";
}
rv.setTextViewText(R.id.widget_item_temp_high,maxtemp);
rv.setTextViewText(R.id.widget_item_temp_low,mintemp);
Intent fillInIntent = new Intent();
fillInIntent.putExtra("currentForcast", forecastList.get(i));
rv.setOnClickFillInIntent(R.id.widget_item_layout, fillInIntent);
return rv;
}
@Override
public RemoteViews getLoadingView() {
return null;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public boolean hasStableIds() {
return true;
}
}

View File

@@ -1,281 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.AppWidget;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.RemoteViews;
import com.appttude.h_mal.atlas_weather.R;
import com.appttude.h_mal.atlas_weather.legacy.data.sql.ForecastDBHelper;
import com.appttude.h_mal.atlas_weather.legacy.model.CurrentForecast;
import com.appttude.h_mal.atlas_weather.legacy.model.ForecastItem;
import com.appttude.h_mal.atlas_weather.legacy.services.location.getLatLong;
import com.appttude.h_mal.atlas_weather.legacy.ui.FurtherInfoActivity;
import java.io.IOException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import static com.appttude.h_mal.atlas_weather.legacy.data.network.RetrieveJSON.UriBuilder;
import static com.appttude.h_mal.atlas_weather.legacy.data.network.RetrieveJSON.createUrl;
import static com.appttude.h_mal.atlas_weather.legacy.data.network.RetrieveJSON.extractFeatureFromJson;
import static com.appttude.h_mal.atlas_weather.legacy.data.network.RetrieveJSON.makeHttpRequest;
import static com.appttude.h_mal.atlas_weather.legacy.data.sql.ForecastContract.ForecastEntry.COLUMN_FORECAST_NAME;
import static com.appttude.h_mal.atlas_weather.legacy.data.sql.ForecastContract.ForecastEntry.COLUMN_WIDGET_FORECAST_ITEM;
import static com.appttude.h_mal.atlas_weather.legacy.data.sql.ForecastContract.ForecastEntry.TABLE_NAME_WIDGET;
import static com.appttude.h_mal.atlas_weather.legacy.ui.home.MainActivity.changeToInt;
import static com.appttude.h_mal.atlas_weather.legacy.ui.home.MainActivity.getImageResource;
import static com.appttude.h_mal.atlas_weather.legacy.ui.home.MainActivity.getLocationName;
/**
* Implementation of App Widget functionality.
*/
public class NewAppWidget extends AppWidgetProvider{
private static String TAG = NewAppWidget.class.getSimpleName();
private static int request = 0;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
getLatLong.configLatLong(context);
forecastAsyncTask task = new forecastAsyncTask(context,appWidgetManager,appWidgetId);
task.execute(UriBuilder(6));
Log.i(TAG, "onUpdate: widget onUpdate called at " + getCurrentTimeUsingDate());
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onEnabled(Context context) {
try {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), NewAppWidget.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.widget_listview);
Log.i(TAG, "onEnabled: called at " + getCurrentTimeUsingDate());
}catch (Exception e){
Log.e(TAG, "onEnabled: ", e);
}
// Enter relevant functionality for when the first widget is created
// getLatLong.configLatLong(context);
// forecastAsyncTask task = new forecastAsyncTask();
// task.execute(UriBuilder(5));
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(
AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), NewAppWidget.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.widget_listview);
Log.i(TAG, "onReceive: widget onReceive called at " + getCurrentTimeUsingDate());
}
super.onReceive(context, intent);
}
static class forecastAsyncTask extends AsyncTask<String,Void,String>{
private Context context;
AppWidgetManager appWidgetManager;
int appWidgetId;
public forecastAsyncTask(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
this.context = context;
this.appWidgetManager = appWidgetManager;
this.appWidgetId = appWidgetId;
}
@Override
protected String doInBackground(String... urlString) {
Log.i(TAG, "doInBackground: started at " + getCurrentTimeUsingDate());
String jsonResponse = null;
if (urlString.length < 1 || urlString[0] == null) {
return null;
}
try {
URL url = createUrl(urlString[0]);
jsonResponse = makeHttpRequest(url);
} catch (IOException e) {
Log.e( TAG, "Problem making the HTTP request.", e);
}
return jsonResponse;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.i(TAG, "onPostExecute: SQL data result: " + result);
if(result == null){
Log.i(TAG, "onPostExecute: result null");
}
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
Intent intentUpdate = new Intent(context, NewAppWidget.class);
intentUpdate.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
int[] idArray = new int[]{appWidgetId};
intentUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, idArray);
PendingIntent pendingUpdate = PendingIntent.getBroadcast(
context, request++, intentUpdate,
PendingIntent.FLAG_UPDATE_CURRENT);
// float opacity = 0.3f; //opacity = 0: fully transparent, opacity = 1: no transparancy
// int backgroundColor = 0x000000; //background color (here black)
// views.setInt( R.id.whole_widget_view, "setBackgroundColor", (int)(opacity * 0xFF) << 24 | backgroundColor);
if (result != null) {
ForecastDBHelper forecastsDbhelper = new ForecastDBHelper(context);
SQLiteDatabase database = forecastsDbhelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_FORECAST_NAME, "Current");
values.put(COLUMN_WIDGET_FORECAST_ITEM, result);
String[] projection = {COLUMN_FORECAST_NAME, COLUMN_WIDGET_FORECAST_ITEM};
Cursor cursor = null;
try {
cursor = database.query(TABLE_NAME_WIDGET,
projection,
COLUMN_FORECAST_NAME + " IS ?",
new String[]{"Current"},
null,
null,
null);
} catch (Exception e) {
Log.e(TAG, "onPostExecute: ", e);
} finally {
if(cursor != null){
while (cursor.moveToNext()){
String currentDB = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_WIDGET_FORECAST_ITEM));
Log.i(TAG, "onPostExecute: current db: " + currentDB);
}
}
if (cursor != null && cursor.getCount() > 0) {
database.update(TABLE_NAME_WIDGET, values, COLUMN_FORECAST_NAME + " IS ?", new String[]{"Current"});
Log.i(TAG, "onPostExecute: attempted to update sql, size:" + cursor.getCount());
cursor.close();
} else {
database.insert(TABLE_NAME_WIDGET, null, values);
Log.i(TAG, "onPostExecute: attempted to insert sql");
}
}
ForecastItem forecastItem = extractFeatureFromJson(result);
CurrentForecast cf = forecastItem.getCurrentForecast();
Intent intent = new Intent(context, WidgetRemoteViewsService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
SharedPreferences mSettings = PreferenceManager.getDefaultSharedPreferences(context);
String temp;
String unit;
if (mSettings.getString("temp_units","").equals("°F")){
temp =changeToInt(cf.getTemp_f());
unit = "°F";
}else {
temp = changeToInt(cf.getTemp_c());
unit = "°C";
}
views.setRemoteAdapter(R.id.widget_listview, intent);
views.setTextViewText(R.id.widget_main_temp,temp);
views.setTextViewText(R.id.widget_feel_temp,unit);
views.setTextViewText(R.id.dash," / ");
views.setTextViewText(R.id.widget_item_temp_high,changeToInt(forecastItem.getForecastArrayList().get(0).getMaxtemp_c())+"°");
views.setTextViewText(R.id.widget_item_temp_low,changeToInt(forecastItem.getForecastArrayList().get(0).getMintemp_c())+"°");
views.setTextViewText(R.id.widget_current_location,getLocationName(context,cf.getLatitude(),cf.getLongitude()));
views.setImageViewResource(R.id.location_icon, R.drawable.location_flag);
views.setImageViewResource(R.id.widget_current_icon,
getImageResource(cf.getIconURL(),context));
Intent clickIntentTemplate = new Intent(context, FurtherInfoActivity.class);
PendingIntent clickPendingIntentTemplate = TaskStackBuilder.create(context)
.addNextIntentWithParentStack(clickIntentTemplate)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
views.setPendingIntentTemplate(R.id.widget_listview, clickPendingIntentTemplate);
views.setOnClickPendingIntent(R.id.widget_current_icon, pendingUpdate);
views.setOnClickPendingIntent(R.id.widget_current_location,pendingUpdate);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.widget_listview);
Log.i(TAG, "onPostExecute: finished at " + getCurrentTimeUsingDate());
}
else {
Log.i(TAG, "onPostExecute: null part executed");
views.setTextViewText(R.id.widget_current_location,"Refresh");
views.setImageViewResource(R.id.widget_current_icon, R.drawable.widget_error_icon);
views.setImageViewResource(R.id.location_icon, R.drawable.refreshing);
views.setOnClickPendingIntent(R.id.widget_current_icon, pendingUpdate);
views.setOnClickPendingIntent(R.id.widget_current_location,pendingUpdate);
appWidgetManager.updateAppWidget(appWidgetId, views);
// appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.widget_listview);
//add a listener to the view
}
}
}
public static String getCurrentTimeUsingDate() {
Date date = new Date();
String strDateFormat = "hh:mm:ss.SSS";
DateFormat dateFormat = new SimpleDateFormat(strDateFormat);
return dateFormat.format(date);
}
}

View File

@@ -1,13 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.AppWidget;
import android.content.Intent;
import android.widget.RemoteViewsService;
public class WidgetRemoteViewsService extends RemoteViewsService{
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new MyWidgetRemoteViewsFactory(getApplicationContext(),intent);
}
}

View File

@@ -1,65 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import androidx.loader.content.AsyncTaskLoader;
import com.appttude.h_mal.atlas_weather.legacy.model.ForecastItem;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import static com.appttude.h_mal.atlas_weather.legacy.data.network.RetrieveJSON.createUrl;
import static com.appttude.h_mal.atlas_weather.legacy.data.network.RetrieveJSON.extractFeatureFromJson;
import static com.appttude.h_mal.atlas_weather.legacy.data.network.RetrieveJSON.makeHttpRequest;
public class ForecastLoader extends AsyncTaskLoader<List<ForecastItem>> {
private ArrayList<String> mUrl;
public ForecastLoader(Context context, ArrayList<String> url) {
super(context);
this.mUrl = url;
}
@Override
protected void onStartLoading() {
forceLoad();
}
@Override
public List<ForecastItem> loadInBackground() {
if (mUrl == null) {
Log.i("", "loadInBackground: " + "null");
return null;
}
String json = null;
List<ForecastItem> forecastItems = new ArrayList<ForecastItem>();
for (int i = 0; i < mUrl.size(); i++) {
try {
URL url = createUrl(mUrl.get(i));
json = makeHttpRequest(url);
} catch (IOException e) {
Log.e("", "Problem making the HTTP request.", e);
}finally {
if (!TextUtils.isEmpty(json)) {
forecastItems.add(extractFeatureFromJson(json));
}
}
}
if (forecastItems.size() < 1){
return null;
}
return forecastItems;
}
}

View File

@@ -1,226 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.data.network;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import com.appttude.h_mal.atlas_weather.legacy.model.CurrentForecast;
import com.appttude.h_mal.atlas_weather.legacy.model.Forecast;
import com.appttude.h_mal.atlas_weather.legacy.model.ForecastItem;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import static com.appttude.h_mal.atlas_weather.legacy.services.location.getLatLong.latitude;
import static com.appttude.h_mal.atlas_weather.legacy.services.location.getLatLong.longitude;
/**
* Created by h_mal on 05/05/2018.
*/
public class RetrieveJSON {
public RetrieveJSON(){}
protected static String APIkey = "1fe09c8cd3c42e573c5cc7c32b27a1b4";
public static String UriBuilder(int days){
String latLong = latitude + "," + longitude;
Uri.Builder builder = new Uri.Builder();
builder.scheme("http")
.authority("api.weatherstack.com")
.appendPath("current")
.appendQueryParameter("access_key",APIkey)
.appendQueryParameter("query",latLong);
return builder.build().toString().replace("%2C",",");
}
public static String UriBuilder(String q){
Uri.Builder builder = new Uri.Builder();
builder.scheme("http")
.authority("api.apixu.com")
.appendPath("v1")
.appendPath("forecast.json")
.appendQueryParameter("key",APIkey)
.appendQueryParameter("q",q)
.appendQueryParameter("days","7");
return builder.build().toString().replace("%2C",",");
}
public static URL createUrl(String stringUrl) {
URL url = null;
try {
url = new URL(stringUrl);
} catch (MalformedURLException e) {
Log.e("ERROR", "Error with creating URL ", e);
}
return url;
}
public static String makeHttpRequest(URL url) throws IOException {
String jsonResponse = null;
if (url == null) {
return jsonResponse;
}
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(30000);
urlConnection.setConnectTimeout(30000);
urlConnection.setRequestMethod("GET");
urlConnection.connect();
if (urlConnection.getResponseCode() == 200) {
inputStream = urlConnection.getInputStream();
jsonResponse = readFromStream(inputStream);
} else {
Log.e("", "Error response code: " + urlConnection.getResponseCode());
}
} catch (IOException e) {
Log.e("", "Problem retrieving the JSON results.", e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (inputStream != null) {
inputStream.close();
}
}
return jsonResponse;
}
private static String readFromStream(InputStream inputStream) throws IOException {
StringBuilder output = new StringBuilder();
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputStreamReader);
String line = "";
while (line != null) {
output.append(line);
line = reader.readLine();
}
}
Log.d("", output.toString());
return output.toString();
}
public static ForecastItem extractFeatureFromJson(String newsJSON) {
if (TextUtils.isEmpty(newsJSON)) {
return null;
}
CurrentForecast forecastCurrent = new CurrentForecast();
List<Forecast> forecasts = new ArrayList<Forecast>();
try {
JSONObject baseJsonResponse = new JSONObject(newsJSON);
JSONObject locationObject = baseJsonResponse.getJSONObject("location");
Double latitude = locationObject.getDouble("lat");
Double longitude = locationObject.getDouble("lon");
String location = locationObject.getString("name");
int last_updated_epoch = locationObject.getInt("localtime_epoch");
JSONObject currentObject = baseJsonResponse.getJSONObject("current");
Double temp_c = currentObject.getDouble("temp_c");
Double temp_f = currentObject.getDouble("temp_f");
JSONObject conditionObject = currentObject.getJSONObject("condition");
String condition_text = conditionObject.getString("text");
String iconURL = conditionObject.getString("icon");
Double wind_mph = currentObject.getDouble("wind_mph");
Double wind_kph = currentObject.getDouble("wind_kph");
String wind_dir = currentObject.getString("wind_dir");
Double pressure_mb = currentObject.getDouble("pressure_mb");
Double pressure_in = currentObject.getDouble("pressure_in");
Double precip_mm = currentObject.getDouble("precip_mm");
Double precip_in = currentObject.getDouble("precip_in");
Double humidity = currentObject.getDouble("humidity");
Double cloud = currentObject.getDouble("cloud");
Double feelslike_c = currentObject.getDouble("feelslike_c");
Double feelslike_f = currentObject.getDouble("feelslike_f");
Double vis_km = currentObject.getDouble("vis_km");
Double vis_miles = currentObject.getDouble("vis_miles");
forecastCurrent = new CurrentForecast(location, latitude, longitude,
last_updated_epoch, temp_c, temp_f, condition_text,
iconURL, wind_mph, wind_kph, wind_dir,
pressure_mb, pressure_in, precip_mm, precip_in,
humidity, cloud, feelslike_c, feelslike_f,
vis_km, vis_miles
);
JSONObject forecastObject = baseJsonResponse.getJSONObject("forecast");
JSONArray forecastsArray = forecastObject.getJSONArray("forecastday");
for (int i = 0; i < forecastsArray.length(); i++) {
JSONObject currentForecastObject = forecastsArray.getJSONObject(i);
Long date = currentForecastObject.getLong("date_epoch");
JSONObject day = currentForecastObject.getJSONObject("day");
Double maxtemp_c = day.getDouble ("maxtemp_c");
Double maxtemp_f = day.getDouble ("maxtemp_f");
Double mintemp_c = day.getDouble ("mintemp_c");
Double mintemp_f = day.getDouble ("mintemp_f");
Double avgtemp_c = day.getDouble ("avgtemp_c");
Double avgtemp_f = day.getDouble ("avgtemp_f");
Double maxwind_mph = day.getDouble ("maxwind_mph");
Double maxwind_kph = day.getDouble ("maxwind_kph");
Double totalprecip_mm = day.getDouble ("totalprecip_mm");
Double totalprecip_in = day.getDouble ("totalprecip_in");
Double avgvis_km = day.getDouble ("avgvis_km");
Double avgvis_miles = day.getDouble ("avgvis_miles");
Double avghumidity = day.getDouble ("avghumidity");
Double uv = day.getDouble("uv");
JSONObject condition = day.getJSONObject("condition");
String conditionText = condition.getString("text");
String imageURL = condition.getString("icon");
JSONObject astro = currentForecastObject.getJSONObject("astro");
String sunrise = astro.getString("sunrise");
String sunset = astro.getString("sunset");
String moonrise = astro.getString("moonrise");
String moonset = astro.getString("moonset");
Forecast forecast = new Forecast(date, maxtemp_c, maxtemp_f, mintemp_c, mintemp_f, avgtemp_c, avgtemp_f,
maxwind_mph, maxwind_kph, totalprecip_mm, totalprecip_in, avgvis_km, avgvis_miles,
avghumidity, conditionText, imageURL, uv, sunrise, sunset, moonrise, moonset)
;
forecasts.add(forecast);
}
} catch (JSONException e) {
Log.e("Error", "Problem parsing the book JSON results", e);
}
return new ForecastItem(forecastCurrent,forecasts);
}
}

View File

@@ -1,45 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.data.sql;
import android.content.ContentResolver;
import android.net.Uri;
import android.provider.BaseColumns;
public class ForecastContract {
public ForecastContract() {
}
public static final String CONTENT_AUTHORITY = "com.appttude.h_mal.atlas_weather";
public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
public static final String PATH_FORECASTS = "forecasts";
public static final String PATH_FORECASTS_WIDGET = "widgetitems";
public static final class ForecastEntry implements BaseColumns {
public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_FORECASTS);
public static final Uri CONTENT_URI_WIDGET = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_FORECASTS_WIDGET);
public static final String CONTENT_LIST_TYPE =
ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_FORECASTS;
public static final String CONTENT_ITEM_TYPE =
ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_FORECASTS;
public final static String TABLE_NAME = "forecasts";
public final static String TABLE_NAME_WIDGET = "widgetitems";
public final static String _ID = BaseColumns._ID;
public final static String COLUMN_FORECAST_NAME = "name";
public final static String COLUMN_WIDGET_FORECAST_ITEM = "widgetforcastitem";
}
}

View File

@@ -1,37 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.data.sql;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.appttude.h_mal.atlas_weather.legacy.data.sql.ForecastContract.ForecastEntry;
public class ForecastDBHelper extends SQLiteOpenHelper {
public static final String name = "forecasts.db";
public static final int version = 1;
public ForecastDBHelper(Context context) {
super(context, name, null, version);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String SQL_CREATE_PRODUCTS_TABLE = "CREATE TABLE " + ForecastEntry.TABLE_NAME + " ("
+ ForecastEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ ForecastEntry.COLUMN_FORECAST_NAME + " TEXT NOT NULL)";
sqLiteDatabase.execSQL(SQL_CREATE_PRODUCTS_TABLE);
sqLiteDatabase.execSQL(SQL_CREATE_PRODUCTS_TABLE_2);
}
private static final String SQL_CREATE_PRODUCTS_TABLE_2 = "CREATE TABLE " + ForecastEntry.TABLE_NAME_WIDGET + " ("
+ ForecastEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ ForecastEntry.COLUMN_FORECAST_NAME + " TEXT NOT NULL, "
+ ForecastEntry.COLUMN_WIDGET_FORECAST_ITEM + " TEXT)";
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
}
}

View File

@@ -1,179 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.data.sql;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.appttude.h_mal.atlas_weather.legacy.data.sql.ForecastContract.ForecastEntry;
public class ForecastProvider extends ContentProvider {
public static final String LOG_TAG = ForecastProvider.class.getSimpleName();
private static final int FORECASTS = 100;
private static final int FORECAST_ID = 101;
private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
sUriMatcher.addURI(ForecastContract.CONTENT_AUTHORITY, ForecastContract.PATH_FORECASTS, FORECASTS);
sUriMatcher.addURI(ForecastContract.CONTENT_AUTHORITY, ForecastContract.PATH_FORECASTS + "/#", FORECAST_ID);
}
ForecastDBHelper mDbHelper;
private SQLiteDatabase database;
@Override
public boolean onCreate() {
mDbHelper = new ForecastDBHelper(getContext());
database = mDbHelper.getReadableDatabase();
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
Cursor cursor;
int match = sUriMatcher.match(uri);
switch (match) {
case FORECASTS:
cursor = database.query(ForecastEntry.TABLE_NAME, projection, selection, selectionArgs,
null, null, sortOrder);
break;
case FORECAST_ID:
selection = ForecastEntry._ID + "=?";
selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };
cursor = database.query(ForecastEntry.TABLE_NAME, projection, selection, selectionArgs,
null, null, sortOrder);
break;
default:
throw new IllegalArgumentException("Cannot query " + uri);
}
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
final int match = sUriMatcher.match(uri);
switch (match) {
case FORECASTS:
return ForecastEntry.CONTENT_LIST_TYPE;
case FORECAST_ID:
return ForecastEntry.CONTENT_ITEM_TYPE;
default:
throw new IllegalStateException("Unknown URI " + uri + " with match " + match);
}
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
final int match = sUriMatcher.match(uri);
switch (match) {
case FORECASTS:
String name = values.getAsString(ForecastEntry.COLUMN_FORECAST_NAME);
if (name == null) {
throw new IllegalArgumentException("name required");
}
SQLiteDatabase database = mDbHelper.getWritableDatabase();
long id = database.insert(ForecastEntry.TABLE_NAME, null, values);
if (id == -1) {
Log.e(LOG_TAG, "row failed " + uri);
return null;
}
getContext().getContentResolver().notifyChange(uri, null);
return ContentUris.withAppendedId(uri, id);
default:
throw new IllegalArgumentException("Insertion is not supported for " + uri);
}
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
SQLiteDatabase database = mDbHelper.getWritableDatabase();
int rowsDeleted;
final int match = sUriMatcher.match(uri);
switch (match) {
case FORECASTS:
rowsDeleted = database.delete(ForecastEntry.TABLE_NAME, selection, selectionArgs);
break;
case FORECAST_ID:
selection = ForecastEntry._ID + "=?";
selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };
rowsDeleted = database.delete(ForecastEntry.TABLE_NAME, selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("Deletion is not supported for " + uri);
}
if (rowsDeleted != 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return rowsDeleted;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String selection, @Nullable String[] selectionArgs) {
final int match = sUriMatcher.match(uri);
switch (match) {
case FORECASTS:
return updateProduct(uri, contentValues, selection, selectionArgs);
case FORECAST_ID:
selection = ForecastEntry._ID + "=?";
selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };
return updateProduct(uri, contentValues, selection, selectionArgs);
default:
throw new IllegalArgumentException("Update is not supported for " + uri);
}
}
private int updateProduct(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
if (values.containsKey(ForecastEntry.COLUMN_FORECAST_NAME)) {
String name = values.getAsString(ForecastEntry.COLUMN_FORECAST_NAME);
if (name == null) {
throw new IllegalArgumentException("Name required");
}
}
if (values.size() == 0) {
return 0;
}
SQLiteDatabase database = mDbHelper.getWritableDatabase();
int rowsUpdated = database.update(ForecastEntry.TABLE_NAME, values, selection, selectionArgs);
if (rowsUpdated != 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return rowsUpdated;
}
}

View File

@@ -1,358 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.model;
import android.os.Parcel;
import android.os.Parcelable;
public class CurrentForecast implements Parcelable{
private String location;
private Double latitude;
private Double longitude;
private int last_updated_epoch;
private Double temp_c;
private Double temp_f;
private String condition_text;
private String iconURL;
private Double wind_mph;
private Double wind_kph;
private String wind_dir;
private Double pressure_mb;
private Double pressure_in;
private Double precip_mm;
private Double precip_in;
private Double humidity;
private Double cloud;
private Double feelslike_c;
private Double feelslike_f;
private Double vis_km;
private Double vis_miles;
public CurrentForecast() {
}
public CurrentForecast(String location, Double latitude, Double longitude, int last_updated_epoch,
Double temp_c, Double temp_f, String condition_text, String iconURL, Double wind_mph,
Double wind_kph, String wind_dir, Double pressure_mb, Double pressure_in, Double precip_mm,
Double precip_in, Double humidity, Double cloud, Double feelslike_c, Double feelslike_f,
Double vis_km, Double vis_miles) {
this.location = location;
this.latitude = latitude;
this.longitude = longitude;
this.last_updated_epoch = last_updated_epoch;
this.temp_c = temp_c;
this.temp_f = temp_f;
this.condition_text = condition_text;
this.iconURL = iconURL;
this.wind_mph = wind_mph;
this.wind_kph = wind_kph;
this.wind_dir = wind_dir;
this.pressure_mb = pressure_mb;
this.pressure_in = pressure_in;
this.precip_mm = precip_mm;
this.precip_in = precip_in;
this.humidity = humidity;
this.cloud = cloud;
this.feelslike_c = feelslike_c;
this.feelslike_f = feelslike_f;
this.vis_km = vis_km;
this.vis_miles = vis_miles;
}
public String getLocation() {
return location;
}
public Double getLatitude() {
return latitude;
}
public Double getLongitude() {
return longitude;
}
public int getLast_updated_epoch() {
return last_updated_epoch;
}
public Double getTemp_c() {
return temp_c;
}
public Double getTemp_f() {
return temp_f;
}
public String getCondition_text() {
return condition_text;
}
public String getIconURL() {
return iconURL;
}
public Double getWind_mph() {
return wind_mph;
}
public Double getWind_kph() {
return wind_kph;
}
public String getWind_dir() {
return wind_dir;
}
public Double getPressure_mb() {
return pressure_mb;
}
public Double getPressure_in() {
return pressure_in;
}
public Double getPrecip_mm() {
return precip_mm;
}
public Double getPrecip_in() {
return precip_in;
}
public Double getHumidity() {
return humidity;
}
public Double getCloud() {
return cloud;
}
public Double getFeelslike_c() {
return feelslike_c;
}
public Double getFeelslike_f() {
return feelslike_f;
}
public Double getVis_km() {
return vis_km;
}
public Double getVis_miles() {
return vis_miles;
}
public static Creator<CurrentForecast> getCREATOR() {
return CREATOR;
}
protected CurrentForecast(Parcel in) {
location = in.readString();
if (in.readByte() == 0) {
latitude = null;
} else {
latitude = in.readDouble();
}
if (in.readByte() == 0) {
longitude = null;
} else {
longitude = in.readDouble();
}
last_updated_epoch = in.readInt();
if (in.readByte() == 0) {
temp_c = null;
} else {
temp_c = in.readDouble();
}
if (in.readByte() == 0) {
temp_f = null;
} else {
temp_f = in.readDouble();
}
condition_text = in.readString();
iconURL = in.readString();
if (in.readByte() == 0) {
wind_mph = null;
} else {
wind_mph = in.readDouble();
}
if (in.readByte() == 0) {
wind_kph = null;
} else {
wind_kph = in.readDouble();
}
wind_dir = in.readString();
if (in.readByte() == 0) {
pressure_mb = null;
} else {
pressure_mb = in.readDouble();
}
if (in.readByte() == 0) {
pressure_in = null;
} else {
pressure_in = in.readDouble();
}
if (in.readByte() == 0) {
precip_mm = null;
} else {
precip_mm = in.readDouble();
}
if (in.readByte() == 0) {
precip_in = null;
} else {
precip_in = in.readDouble();
}
if (in.readByte() == 0) {
humidity = null;
} else {
humidity = in.readDouble();
}
if (in.readByte() == 0) {
cloud = null;
} else {
cloud = in.readDouble();
}
if (in.readByte() == 0) {
feelslike_c = null;
} else {
feelslike_c = in.readDouble();
}
if (in.readByte() == 0) {
feelslike_f = null;
} else {
feelslike_f = in.readDouble();
}
if (in.readByte() == 0) {
vis_km = null;
} else {
vis_km = in.readDouble();
}
if (in.readByte() == 0) {
vis_miles = null;
} else {
vis_miles = in.readDouble();
}
}
public static final Creator<CurrentForecast> CREATOR = new Creator<CurrentForecast>() {
@Override
public CurrentForecast createFromParcel(Parcel in) {
return new CurrentForecast(in);
}
@Override
public CurrentForecast[] newArray(int size) {
return new CurrentForecast[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(location);
if (latitude == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(latitude);
}
if (longitude == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(longitude);
}
parcel.writeInt(last_updated_epoch);
if (temp_c == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(temp_c);
}
if (temp_f == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(temp_f);
}
parcel.writeString(condition_text);
parcel.writeString(iconURL);
if (wind_mph == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(wind_mph);
}
if (wind_kph == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(wind_kph);
}
parcel.writeString(wind_dir);
if (pressure_mb == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(pressure_mb);
}
if (pressure_in == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(pressure_in);
}
if (precip_mm == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(precip_mm);
}
if (precip_in == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(precip_in);
}
if (humidity == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(humidity);
}
if (cloud == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(cloud);
}
if (feelslike_c == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(feelslike_c);
}
if (feelslike_f == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(feelslike_f);
}
if (vis_km == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(vis_km);
}
if (vis_miles == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(vis_miles);
}
}
}

View File

@@ -1,351 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.model;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by h_mal on 19/04/2018.
*/
public class Forecast implements Parcelable {
private Long date_epoch; //dt
private Double maxtemp_c; //temp.max
private Double maxtemp_f;
private Double mintemp_c; //temp.min
private Double mintemp_f;
private Double avgtemp_c;
private Double avgtemp_f;
private Double maxwind_mph; //wind_speed
private Double maxwind_kph;
private Double totalprecip_mm;
private Double totalprecip_in;
private Double avgvis_km;
private Double avgvis_miles;
private Double avghumidity; //humidity
private String forecast_text; //weather.[0].description
private String iconURL; //weather.[0].icon
private Double uv; //uvi
private String sunrise; //sunrise
private String sunset; //sunset
private String moonrise;
private String moonset;
public Forecast(Long date_epoch, Double maxtemp_c, Double maxtemp_f, Double mintemp_c, Double mintemp_f, Double avgtemp_c, Double avgtemp_f,
Double maxwind_mph, Double maxwind_kph, Double totalprecip_mm, Double totalprecip_in, Double avgvis_km, Double avgvis_miles,
Double avghumidity, String forecast_text, String iconURL, Double uv, String sunrise, String sunset, String moonrise, String moonset) {
this.date_epoch = date_epoch;
this.maxtemp_c = maxtemp_c;
this.maxtemp_f = maxtemp_f;
this.mintemp_c = mintemp_c;
this.mintemp_f = mintemp_f;
this.avgtemp_c = avgtemp_c;
this.avgtemp_f = avgtemp_f;
this.maxwind_mph = maxwind_mph;
this.maxwind_kph = maxwind_kph;
this.totalprecip_mm = totalprecip_mm;
this.totalprecip_in = totalprecip_in;
this.avgvis_km = avgvis_km;
this.avgvis_miles = avgvis_miles;
this.avghumidity = avghumidity;
this.forecast_text = forecast_text;
this.iconURL = iconURL;
this.uv = uv;
this.sunrise = sunrise;
this.sunset = sunset;
this.moonrise = moonrise;
this.moonset = moonset;
}
public Forecast(Long date_epoch, Double avgtemp_c,String forecast_text, String iconURL) {
this.date_epoch = date_epoch;
this.avgtemp_c = avgtemp_c;
this.forecast_text = forecast_text;
this.iconURL = iconURL;
}
protected Forecast(Parcel in) {
if (in.readByte() == 0) {
date_epoch = null;
} else {
date_epoch = in.readLong();
}
if (in.readByte() == 0) {
maxtemp_c = null;
} else {
maxtemp_c = in.readDouble();
}
if (in.readByte() == 0) {
maxtemp_f = null;
} else {
maxtemp_f = in.readDouble();
}
if (in.readByte() == 0) {
mintemp_c = null;
} else {
mintemp_c = in.readDouble();
}
if (in.readByte() == 0) {
mintemp_f = null;
} else {
mintemp_f = in.readDouble();
}
if (in.readByte() == 0) {
avgtemp_c = null;
} else {
avgtemp_c = in.readDouble();
}
if (in.readByte() == 0) {
avgtemp_f = null;
} else {
avgtemp_f = in.readDouble();
}
if (in.readByte() == 0) {
maxwind_mph = null;
} else {
maxwind_mph = in.readDouble();
}
if (in.readByte() == 0) {
maxwind_kph = null;
} else {
maxwind_kph = in.readDouble();
}
if (in.readByte() == 0) {
totalprecip_mm = null;
} else {
totalprecip_mm = in.readDouble();
}
if (in.readByte() == 0) {
totalprecip_in = null;
} else {
totalprecip_in = in.readDouble();
}
if (in.readByte() == 0) {
avgvis_km = null;
} else {
avgvis_km = in.readDouble();
}
if (in.readByte() == 0) {
avgvis_miles = null;
} else {
avgvis_miles = in.readDouble();
}
if (in.readByte() == 0) {
avghumidity = null;
} else {
avghumidity = in.readDouble();
}
forecast_text = in.readString();
iconURL = in.readString();
if (in.readByte() == 0) {
uv = null;
} else {
uv = in.readDouble();
}
sunrise = in.readString();
sunset = in.readString();
moonrise = in.readString();
moonset = in.readString();
}
public static final Creator<Forecast> CREATOR = new Creator<Forecast>() {
@Override
public Forecast createFromParcel(Parcel in) {
return new Forecast(in);
}
@Override
public Forecast[] newArray(int size) {
return new Forecast[size];
}
};
public Long getDate_epoch() {
return date_epoch;
}
public Double getMaxtemp_c() {
return maxtemp_c;
}
public Double getMaxtemp_f() {
return maxtemp_f;
}
public Double getMintemp_c() {
return mintemp_c;
}
public Double getMintemp_f() {
return mintemp_f;
}
public Double getAvgtemp_c() {
return avgtemp_c;
}
public Double getAvgtemp_f() {
return avgtemp_f;
}
public Double getMaxwind_mph() {
return maxwind_mph;
}
public Double getMaxwind_kph() {
return maxwind_kph;
}
public Double getTotalprecip_mm() {
return totalprecip_mm;
}
public Double getTotalprecip_in() {
return totalprecip_in;
}
public Double getAvgvis_km() {
return avgvis_km;
}
public Double getAvgvis_miles() {
return avgvis_miles;
}
public Double getAvghumidity() {
return avghumidity;
}
public String getForecast_text() {
return forecast_text;
}
public String getIconURL() {
return iconURL;
}
public Double getUv() {
return uv;
}
public String getSunrise() {
return sunrise;
}
public String getSunset() {
return sunset;
}
public String getMoonrise() {
return moonrise;
}
public String getMoonset() {
return moonset;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
if (date_epoch == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeLong(date_epoch);
}
if (maxtemp_c == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(maxtemp_c);
}
if (maxtemp_f == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(maxtemp_f);
}
if (mintemp_c == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(mintemp_c);
}
if (mintemp_f == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(mintemp_f);
}
if (avgtemp_c == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(avgtemp_c);
}
if (avgtemp_f == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(avgtemp_f);
}
if (maxwind_mph == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(maxwind_mph);
}
if (maxwind_kph == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(maxwind_kph);
}
if (totalprecip_mm == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(totalprecip_mm);
}
if (totalprecip_in == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(totalprecip_in);
}
if (avgvis_km == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(avgvis_km);
}
if (avgvis_miles == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(avgvis_miles);
}
if (avghumidity == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(avghumidity);
}
parcel.writeString(forecast_text);
parcel.writeString(iconURL);
if (uv == null) {
parcel.writeByte((byte) 0);
} else {
parcel.writeByte((byte) 1);
parcel.writeDouble(uv);
}
parcel.writeString(sunrise);
parcel.writeString(sunset);
parcel.writeString(moonrise);
parcel.writeString(moonset);
}
}

View File

@@ -1,53 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.model;
import android.os.Parcel;
import android.os.Parcelable;
import java.util.List;
public class ForecastItem implements Parcelable {
private CurrentForecast currentForecast;
private List<Forecast> forecastArrayList;
public ForecastItem(CurrentForecast currentForecast, List<Forecast> forecastArrayList) {
this.currentForecast = currentForecast;
this.forecastArrayList = forecastArrayList;
}
protected ForecastItem(Parcel in) {
currentForecast = in.readParcelable(CurrentForecast.class.getClassLoader());
forecastArrayList = in.createTypedArrayList(Forecast.CREATOR);
}
public static final Creator<ForecastItem> CREATOR = new Creator<ForecastItem>() {
@Override
public ForecastItem createFromParcel(Parcel in) {
return new ForecastItem(in);
}
@Override
public ForecastItem[] newArray(int size) {
return new ForecastItem[size];
}
};
public CurrentForecast getCurrentForecast() {
return currentForecast;
}
public List<Forecast> getForecastArrayList() {
return forecastArrayList;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeParcelable(currentForecast, i);
parcel.writeTypedList(forecastArrayList);
}
}

View File

@@ -1,119 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.model;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.appttude.h_mal.atlas_weather.R;
import java.util.List;
public class infopageItem {
private int icon;
private String firstLine;
private String authorUrl;
private String authorName;
private String siteUrl;
private String siteName;
public infopageItem(int icon, @Nullable String firstLine, @Nullable String authorUrl, @Nullable String authorName,
@Nullable String siteUrl, @Nullable String siteName) {
this.icon = icon;
this.firstLine = firstLine;
this.authorUrl = authorUrl;
this.authorName = authorName;
this.siteUrl = siteUrl;
this.siteName = siteName;
}
public int getInfoIcon() {
return icon;
}
public String getFirstLine() {
return firstLine;
}
public String getAuthorUrl() {
return authorUrl;
}
public String getAuthorName() {
return authorName;
}
public String getSiteUrl() {
return siteUrl;
}
public String getSiteName() {
return siteName;
}
public static class infoPageAdapter extends ArrayAdapter<infopageItem>{
public infoPageAdapter(@NonNull Context context, @NonNull List<infopageItem> objects) {
super(context, 0, objects);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.info_dialog_item, parent,false);
}
infopageItem inf = getItem(position);
ImageView imageView = listItemView.findViewById(R.id.infopage_icon);
TextView firstLineTextView = listItemView.findViewById(R.id.first_part_text);
TextView authorLinkTextView = listItemView.findViewById(R.id.author_link);
TextView thirdLine = listItemView.findViewById(R.id.third_text);
TextView companyWebsiteTextView = listItemView.findViewById(R.id.company_website);
if(position == 0){
imageView.setImageResource(inf.getInfoIcon());
firstLineTextView.setText(inf.getFirstLine());
authorLinkTextView.setVisibility(View.GONE);
thirdLine.setText("");
SetUrlHyperlink(companyWebsiteTextView,inf.getSiteName(),inf.getSiteUrl());
}else{
imageView.setImageResource(inf.getInfoIcon());
firstLineTextView.setText(inf.getFirstLine());
authorLinkTextView.setVisibility(View.VISIBLE);
thirdLine.setText("From ");
SetUrlHyperlink(authorLinkTextView,inf.getAuthorName(), inf.getAuthorUrl());
SetUrlHyperlink(companyWebsiteTextView,"www.flaticon.com ","www.flaticon.com" );
}
return listItemView;
}
private void SetUrlHyperlink(TextView textView, String textString, final String url){
textView.setText(textString);
textView.setTextColor(getContext().getColor(android.R.color.holo_blue_dark));
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
getContext().startActivity(myIntent);
}
});
}
}
}

View File

@@ -1,69 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.services.location;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;
import android.widget.Toast;
import androidx.core.app.ActivityCompat;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
/**
* Created by h_mal on 05/05/2018.
*/
public class getLatLong {
static Location location;
public static Double longitude;
public static Double latitude;
private static String TAG = getLatLong.class.getSimpleName();
public getLatLong() {
super();
}
public static void configLatLong(Context context) {
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context, "Location permission denied", Toast.LENGTH_SHORT).show();
} else {
try {
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
} catch (Exception e) {
Log.e("latlong error", "configLatLong: ", e);
} finally {
if (location == null) {
Log.i(TAG, "configLatLong: location initially was null");
try {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
} catch (Exception e) {
Log.e(TAG, "configLatLong: ", e);
} finally {
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.i(TAG, "onSuccess: Latitude:" + location.getLatitude()
+ "\n longitude: " + location.getLongitude());
}
}
} else {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}

View File

@@ -1,164 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.services.notifcation;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Icon;
import android.os.AsyncTask;
import android.os.Build;
import android.preference.PreferenceManager;
import android.util.Log;
import com.appttude.h_mal.atlas_weather.R;
import com.appttude.h_mal.atlas_weather.legacy.data.network.RetrieveJSON;
import com.appttude.h_mal.atlas_weather.legacy.model.CurrentForecast;
import com.appttude.h_mal.atlas_weather.legacy.model.ForecastItem;
import com.appttude.h_mal.atlas_weather.legacy.services.location.getLatLong;
import com.appttude.h_mal.atlas_weather.legacy.ui.home.MainActivity;
import java.io.IOException;
import java.net.URL;
import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
import static com.appttude.h_mal.atlas_weather.legacy.data.network.RetrieveJSON.createUrl;
import static com.appttude.h_mal.atlas_weather.legacy.data.network.RetrieveJSON.extractFeatureFromJson;
import static com.appttude.h_mal.atlas_weather.legacy.data.network.RetrieveJSON.makeHttpRequest;
import static com.appttude.h_mal.atlas_weather.legacy.services.location.getLatLong.latitude;
import static com.appttude.h_mal.atlas_weather.legacy.services.location.getLatLong.longitude;
import static com.appttude.h_mal.atlas_weather.legacy.ui.home.MainActivity.changeToInt;
import static com.appttude.h_mal.atlas_weather.legacy.ui.home.MainActivity.getImageResource;
/**
* Created by h_mal on 29/04/2018.
*/
public class NotificationReceiver extends BroadcastReceiver{
private String TAG = getClass().getSimpleName();
private static final String NOTIFICATION_CHANNEL_ID = "my_notification_channel_1";
Context mContext;
@Override
public void onReceive(Context context, Intent intent) {
this.mContext = context;
Log.i(TAG, "onReceive: notif fired");
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(context);
boolean notif = SP.getBoolean("notif_boolean",true);
if(notif) {
try {
getLatLong.configLatLong(mContext);
} catch (Exception e) {
Log.e(TAG, "onReceive: ", e);
} finally {
if (latitude != null && longitude != null) {
String stringURL = RetrieveJSON.UriBuilder(5);
NotifAsyncTask task = new NotifAsyncTask();
task.execute(stringURL);
}
}
}
SP.edit().putBoolean("FIRST_TIME_RUN",false).apply();
}
private void pushNotif(Context context, ForecastItem forecastItem){
Intent notificationIntent = new Intent(context, MainActivity.class);
CurrentForecast currentForecast = forecastItem.getCurrentForecast();
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
SharedPreferences mSettings = PreferenceManager.getDefaultSharedPreferences(context);
String temp;
if (mSettings.getString("temp_units","").equals("°F")){
temp =changeToInt(currentForecast.getTemp_f());
}else {
temp = changeToInt(currentForecast.getTemp_c());
}
Notification.Builder builder = new Notification.Builder(context);
Notification notification = builder.setContentTitle("Weather App")
.setContentText(temp + "° - " + currentForecast.getCondition_text())
.setSmallIcon(R.mipmap.ic_notif) //change icon
.setLargeIcon(Icon.createWithResource(context,getImageResource(forecastItem.getCurrentForecast().getIconURL(),context)))
.setAutoCancel(true)
.setContentIntent(pendingIntent).build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId(NOTIFICATION_CHANNEL_ID);
}
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID,
"NotificationDemo",
IMPORTANCE_DEFAULT
);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notification);
}
private class NotifAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urlString) {
String jsonResponse = null;
if (urlString.length < 1 || urlString[0] == null) {
return null;
}
try {
URL url = createUrl(urlString[0]);
jsonResponse = makeHttpRequest(url);
} catch (IOException e) {
Log.e(TAG, "Problem making the HTTP request.", e);
}
return jsonResponse;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.i(TAG, "onPostExecute: " +result);
if (result != null && !result.isEmpty()) {
final ForecastItem forecastItem = extractFeatureFromJson(result);
pushNotif(mContext,forecastItem);
}
}
}
}

View File

@@ -1,145 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.ui;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.appttude.h_mal.atlas_weather.R;
import com.appttude.h_mal.atlas_weather.legacy.data.network.RetrieveJSON;
import com.appttude.h_mal.atlas_weather.legacy.data.sql.ForecastContract.ForecastEntry;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import static com.appttude.h_mal.atlas_weather.legacy.data.network.RetrieveJSON.createUrl;
public class AddForecastActivity extends AppCompatActivity {
private EditText tv;
private String nameString;
public ProgressBar progBarAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_forecast);
tv = findViewById(R.id.location_name_tv);
progBarAdd = findViewById(R.id.pb_add);
progBarAdd.setVisibility(View.GONE);
Button button = findViewById(R.id.submit);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
nameString = tv.getText().toString().trim();
submitEntry(nameString);
}
});
}
private void submitEntry(String s){
if (TextUtils.isEmpty(s)) {
Toast.makeText(AddForecastActivity.this, "please insert a location name", Toast.LENGTH_SHORT).show();
return;
}
new RetrieveFeedTask().execute(s);
}
private void insertIntoDb(){
ContentValues values = new ContentValues();
values.put(ForecastEntry.COLUMN_FORECAST_NAME, nameString);
Uri newUri = getContentResolver().insert(ForecastEntry.CONTENT_URI, values);
if (newUri == null) {
Toast.makeText(this, getString(R.string.insert_entry_failed),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, getString(R.string.insert_entry_successful),
Toast.LENGTH_SHORT).show();
}
}
public class RetrieveFeedTask extends AsyncTask<String, Void, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progBarAdd.setVisibility(View.VISIBLE);
}
protected Boolean doInBackground(String... urls) {
HttpURLConnection urlConnection = null;
URL url = createUrl(RetrieveJSON.UriBuilder(urls[0]));
Boolean b = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(30000);
urlConnection.setConnectTimeout(30000);
urlConnection.setRequestMethod("GET");
urlConnection.connect();
if (urlConnection.getResponseCode() == 200) {
b = true;
Log.i("", "checkHttpRequest: good response");
} else {
b = false;
Log.e("", "Error response code: " + urlConnection.getResponseCode());
}
} catch (IOException e) {
Log.e("", "Problem retrieving the response results.", e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return b;
}
protected void onPostExecute(Boolean feed) {
progBarAdd.setVisibility(View.GONE);
if (feed == null){
Toast.makeText(AddForecastActivity.this, "No connection", Toast.LENGTH_SHORT).show();
return;
}
if (feed){
insertIntoDb();
Intent returnIntent = new Intent();
returnIntent.putExtra("result", getClass().getSimpleName());
setResult(RESULT_OK, returnIntent);
finish();
}else{
Toast.makeText(AddForecastActivity.this, "Location not found", Toast.LENGTH_SHORT).show();
}
}
}
}

View File

@@ -1,68 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.ui;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.appttude.h_mal.atlas_weather.R;
import com.appttude.h_mal.atlas_weather.legacy.model.Forecast;
import static com.appttude.h_mal.atlas_weather.legacy.ui.home.MainActivity.changeToInt;
public class FurtherInfoActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_further_info);
Intent mIntent = getIntent();
Forecast forecast = mIntent.getParcelableExtra("currentForcast");
SharedPreferences mSettings = PreferenceManager.getDefaultSharedPreferences(this);
TextView maxTemp = findViewById(R.id.maxtemp);
TextView averageTemp = findViewById(R.id.averagetemp);
TextView minimumTemp = findViewById(R.id.minimumtemp);
TextView windText = findViewById(R.id.windtext);
TextView precipText = findViewById(R.id.preciptext);
TextView humidityText = findViewById(R.id.humiditytext);
TextView uvText = findViewById(R.id.uvtext);
TextView sunriseText = findViewById(R.id.sunrisetext);
TextView sunsetText = findViewById(R.id.sunsettext);
if (mSettings.getString("temp_units","").equals("°F")){
maxTemp.setText(changeToInt(forecast.getMaxtemp_f()) + mSettings.getString("temp_units","°F"));
averageTemp.setText(changeToInt(forecast.getAvgtemp_f())+mSettings.getString("temp_units","°F"));
minimumTemp.setText(changeToInt(forecast.getMintemp_f())+mSettings.getString("temp_units","°F"));
}else {
maxTemp.setText(changeToInt(forecast.getMaxtemp_c())+mSettings.getString("temp_units","°C"));
averageTemp.setText(changeToInt(forecast.getAvgtemp_c())+mSettings.getString("temp_units","°C"));
minimumTemp.setText(changeToInt(forecast.getMintemp_c())+mSettings.getString("temp_units","°C"));
}
if (mSettings.getString("wind_units","").equals("mph")){
windText.setText(String.valueOf(forecast.getMaxwind_mph()+mSettings.getString("wind_units","mhp")));
}else {
windText.setText(String.valueOf(forecast.getMaxwind_kph()+mSettings.getString("wind_units","kph")));
}
if (mSettings.getString("precip_units","").equals("in")){
precipText.setText(String.valueOf(forecast.getTotalprecip_in()+mSettings.getString("precip_units","inches")));
}else {
precipText.setText(String.valueOf(forecast.getTotalprecip_mm()+ mSettings.getString("precip_units","mm")));
}
humidityText.setText(String.valueOf(forecast.getAvghumidity()));
uvText.setText(String.valueOf(forecast.getUv()));
sunriseText.setText(forecast.getSunrise());
sunsetText.setText(forecast.getSunset());
}
}

View File

@@ -1,61 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.ui;
import android.os.Bundle;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import com.appttude.h_mal.atlas_weather.R;
import com.appttude.h_mal.atlas_weather.legacy.model.infopageItem;
import java.util.ArrayList;
import java.util.List;
public class InfoActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info_dialog_layout);
List<infopageItem> infopageItemList = new ArrayList<>();
infopageItemList.add(new infopageItem(R.drawable.day_305,
"Weather data and icons provided by: ",null,null,
"https://www.apixu.com/","APIXU"));
infopageItemList.add(new infopageItem(R.drawable.somethingnew,
"Icon made by: ",
"https://www.flaticon.com/authors/hirschwolf",
"Hirschwolf",
null,
null));
infopageItemList.add(new infopageItem(R.drawable.breeze,
"Icon made by: ",
"https://www.flaticon.com/authors/hirschwolf",
"Hirchwolf",
null,null));
infopageItemList.add(new infopageItem(R.drawable.water_drop,
"Icon made by: ",
"https://www.flaticon.com/authors/freepik",
"Freepic",
null,null));
infopageItemList.add(new infopageItem(R.drawable.cloud_symbol,
"Icon made by: ",
"https://www.flaticon.com/authors/simpleicon",
"Simple Icon",
null,null));
infopageItemList.add(new infopageItem(R.drawable.sun,
"Icon made by: ",
"https://www.flaticon.com/authors/freepik",
"Freepic",
null,null));
infopageItemList.add(new infopageItem(R.mipmap.ic_world,
"Icon made by: ",
"https://www.flaticon.com/authors/freepik",
"Freepic",
null,null));
ListView lf = findViewById(R.id.listview_info);
lf.setAdapter(new infopageItem.infoPageAdapter(this,infopageItemList));
}
}

View File

@@ -1,82 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.ui;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import com.appttude.h_mal.atlas_weather.R;
import com.appttude.h_mal.atlas_weather.legacy.AppWidget.NewAppWidget;
import static com.appttude.h_mal.atlas_weather.legacy.ui.home.MainActivity.setupNotificationBroadcaster;
public class UnitSettingsActivity extends PreferenceActivity {
private String TAG = getClass().getSimpleName();
private SharedPreferences.OnSharedPreferenceChangeListener prefListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager.setDefaultValues(this, R.xml.prefs, false);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
//listener on changed sort order preference:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if(key.equals("temp_units")){
Intent intent = new Intent(getBaseContext(), NewAppWidget.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
int[] ids = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), NewAppWidget.class));
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(intent);
}
if(key.equals("notif_boolean")){
setupNotificationBroadcaster(getBaseContext());
}
}
};
prefs.registerOnSharedPreferenceChangeListener(prefListener);
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
// @Override
// public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
// Log.i(TAG, "onSharedPreferenceChanged: " + s);
// if (s == "temp_units"){
// Intent intent = new Intent(getBaseContext(), NewAppWidget.class);
// intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
//
// int[] ids = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), NewAppWidget.class));
// intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
// sendBroadcast(intent);
// }
// }
public static class MyPreferenceFragment extends PreferenceFragment
{
@Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
}

View File

@@ -1,83 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.ui;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.Loader;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.appttude.h_mal.atlas_weather.R;
import com.appttude.h_mal.atlas_weather.legacy.model.ForecastItem;
import com.appttude.h_mal.atlas_weather.legacy.ui.adapters.RecyclerViewAdapter;
import java.util.List;
public class WorldItemActivity extends AppCompatActivity
implements LoaderManager.LoaderCallbacks<List<ForecastItem>> {
private static final int NEWS_LOADER_ID = 1;
RecyclerView forecastsListView;
LinearLayout emptyList;
ProgressBar mainPG;
SwipeRefreshLayout mSwipeRefreshLayout;
private RecyclerViewAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Intent mIntent = getIntent();
ForecastItem forecast = mIntent.getParcelableExtra("ForecastItem");
forecastsListView = findViewById(R.id.forecast_listview);
forecastsListView.setLayoutManager(new LinearLayoutManager(this));
emptyList = findViewById(R.id.emptyView);
mainPG = findViewById(R.id.mainPB);
emptyList.setVisibility(View.GONE);
mainPG.setVisibility(View.GONE);
mAdapter = new RecyclerViewAdapter(this,forecast);
forecastsListView.setAdapter(mAdapter);
mSwipeRefreshLayout = findViewById(R.id.swipe_refresh);
mSwipeRefreshLayout.setRefreshing(false);
// if (networkInfo != null && networkInfo.isConnected()) {
// android.support.v4.app.LoaderManager loaderManager = getSupportLoaderManager();
// loaderManager.initLoader(NEWS_LOADER_ID, null, this);
// } else {
// emptyList.setVisibility(View.VISIBLE);
// mainPG.setVisibility(View.GONE);
// }
}
@Override
public Loader<List<ForecastItem>> onCreateLoader(int id, Bundle args) {
// URL url = createUrl(UriBuilder());
// ArrayList<String> entries = new ArrayList<>();
// entries.add(url.toString());
// return new ForecastLoader(this,entries);
return null;
}
@Override
public void onLoadFinished(Loader<List<ForecastItem>> loader, List<ForecastItem> data) {
}
@Override
public void onLoaderReset(Loader<List<ForecastItem>> loader) {
// loader.reset();
}
}

View File

@@ -1,84 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.ui.adapters;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.appttude.h_mal.atlas_weather.R;
import com.appttude.h_mal.atlas_weather.legacy.model.CurrentForecast;
import com.appttude.h_mal.atlas_weather.legacy.model.ForecastItem;
import com.appttude.h_mal.atlas_weather.legacy.ui.WorldItemActivity;
import java.util.List;
import static com.appttude.h_mal.atlas_weather.legacy.ui.home.MainActivity.changeToInt;
import static com.appttude.h_mal.atlas_weather.legacy.ui.home.MainActivity.getImageResource;
public class CurrentForecastAdapter extends ArrayAdapter<ForecastItem> {
Context context;
private SharedPreferences mSettings;
public CurrentForecastAdapter(@NonNull Context context, @NonNull List<ForecastItem> objects) {
super(context, 0, objects);
this.context = context;
mSettings = PreferenceManager.getDefaultSharedPreferences(context);
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.db_list_item, parent,false);
}
CurrentForecast currentForecast = null;
try {
currentForecast = getItem(position).getCurrentForecast();
}catch (Exception e){
Log.e(getClass().getSimpleName(), "error: ", e);
}finally {
if (currentForecast != null){
TextView location = listItemView.findViewById(R.id.db_location);
location.setText(currentForecast.getLocation());
TextView conditionTV = listItemView.findViewById(R.id.db_condition);
conditionTV.setText(currentForecast.getCondition_text());
ImageView weatherIV = listItemView.findViewById(R.id.db_icon);
weatherIV.setImageResource(getImageResource(currentForecast.getIconURL(), context));
TextView mainTempTV = listItemView.findViewById(R.id.db_main_temp);
TextView tempUnit = listItemView.findViewById(R.id.db_minor_temp);
if (mSettings.getString("temp_units","").equals("°F")){
mainTempTV.setText(changeToInt(currentForecast.getTemp_f()));
tempUnit.setText("°F");
}else {
mainTempTV.setText(changeToInt(currentForecast.getTemp_c()));
tempUnit.setText("°C");
}
}
}
return listItemView;
}
private void openWorldItem(ForecastItem forcast){
Intent i = new Intent(context, WorldItemActivity.class);
i.putExtra("ForecastItem",forcast);
context.startActivity(i);
}
}

View File

@@ -1,262 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.ui.adapters;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.appttude.h_mal.atlas_weather.R;
import com.appttude.h_mal.atlas_weather.legacy.model.CurrentForecast;
import com.appttude.h_mal.atlas_weather.legacy.model.Forecast;
import com.appttude.h_mal.atlas_weather.legacy.model.ForecastItem;
import com.appttude.h_mal.atlas_weather.legacy.ui.FurtherInfoActivity;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import static com.appttude.h_mal.atlas_weather.legacy.ui.home.MainActivity.changeToInt;
import static com.appttude.h_mal.atlas_weather.legacy.ui.home.MainActivity.getImageResource;
import static com.appttude.h_mal.atlas_weather.legacy.ui.home.MainActivity.getLocationName;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private ForecastItem forecastItem;
private CurrentForecast currentForecast;
private List<Forecast> forecast;
private SharedPreferences mSettings;
private String location;
class ViewHolderCurrent extends RecyclerView.ViewHolder {
TextView locationTV;
TextView conditionTV;
ImageView weatherIV;
TextView avgTempTV;
TextView feels;
TextView tempUnit;
public ViewHolderCurrent(View listItemView) {
super(listItemView);
locationTV = listItemView.findViewById(R.id.location_main_4);
conditionTV = listItemView.findViewById(R.id.condition_main_4);
weatherIV = listItemView.findViewById(R.id.icon_main_4);
avgTempTV = listItemView.findViewById(R.id.temp_main_4);
// feels = listItemView.findViewById(R.id.feelstemp);
tempUnit = listItemView.findViewById(R.id.temp_unit_4);
}
}
class ViewHolderForecast extends RecyclerView.ViewHolder {
TextView dateTV;
TextView dayTV;
TextView conditionTV;
ImageView weatherIV;
TextView mainTempTV;
TextView minorTempTV;
public ViewHolderForecast(View itemView){
super(itemView);
dateTV = itemView.findViewById(R.id.list_date);
dayTV = itemView.findViewById(R.id.list_day);
conditionTV = itemView.findViewById(R.id.list_condition);
weatherIV = itemView.findViewById(R.id.list_icon);
mainTempTV = itemView.findViewById(R.id.list_main_temp);
minorTempTV = itemView.findViewById(R.id.list_minor_temp);
}
}
class ViewHolderFurtherDetails extends RecyclerView.ViewHolder {
TextView windSpeed;
TextView windDirection;
TextView precipitation;
TextView humidity;
TextView clouds;
public ViewHolderFurtherDetails(View itemView){
super(itemView);
windSpeed = itemView.findViewById(R.id.windspeed);
windDirection = itemView.findViewById(R.id.winddirection);
precipitation = itemView.findViewById(R.id.precip_);
humidity = itemView.findViewById(R.id.humidity_);
clouds = itemView.findViewById(R.id.clouds_);
}
}
public RecyclerViewAdapter(@NonNull Context context, @NonNull ForecastItem forecastItem, Double latitude, Double longitude) {
this.context = context;
this.forecastItem = forecastItem;
this.currentForecast = forecastItem.getCurrentForecast();
this.location = getLocationName(context,currentForecast.getLatitude(),currentForecast.getLongitude());
List<Forecast> f = forecastItem.getForecastArrayList();
f.remove(0);
forecast = f;
mSettings = PreferenceManager.getDefaultSharedPreferences(context);
}
public RecyclerViewAdapter(@NonNull Context context, @NonNull ForecastItem forecastItem) {
this.context = context;
this.forecastItem = forecastItem;
this.currentForecast = forecastItem.getCurrentForecast();
List<Forecast> f = forecastItem.getForecastArrayList();
f.remove(0);
forecast = f;
mSettings = PreferenceManager.getDefaultSharedPreferences(context);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 1:
View viewCurrent = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item4, parent, false);
return new ViewHolderCurrent(viewCurrent);
case 2:
View viewForecast = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_layout2, parent, false);
return new ViewHolderForecast(viewForecast);
case 3:
View viewFurther = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item3, parent, false);
return new ViewHolderFurtherDetails(viewFurther);
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
switch (holder.getItemViewType()) {
case 1:
final ViewHolderCurrent viewHolderCurrent = (ViewHolderCurrent) holder;
if (location != null) {
viewHolderCurrent.locationTV.setText(location);
}else{
viewHolderCurrent.locationTV.setText(currentForecast.getLocation());
}
viewHolderCurrent.conditionTV.setText(currentForecast.getCondition_text());
viewHolderCurrent.weatherIV.setImageResource(getImageResource(currentForecast.getIconURL(),context));
if (mSettings.getString("temp_units","").equals("°F")){
viewHolderCurrent.avgTempTV.setText(changeToInt(currentForecast.getTemp_f()));
// viewHolderCurrent.feels.setText(changeToInt(currentForecast.getFeelslike_f()));
viewHolderCurrent.tempUnit.setText("°F");
}else {
viewHolderCurrent.avgTempTV.setText(changeToInt(currentForecast.getTemp_c()));
// viewHolderCurrent.feels.setText(changeToInt(currentForecast.getFeelslike_c()));
viewHolderCurrent.tempUnit.setText("°C");
}
break;
case 2:
final ViewHolderForecast viewHolderForecast = (ViewHolderForecast) holder;
final Forecast f = forecast.get(position -1);
Date updatedate = new Date(f.getDate_epoch() * 1000);
SimpleDateFormat format = new SimpleDateFormat("EEEE");
String day = format.format(updatedate);
viewHolderForecast.dayTV.setText(day);
format = new SimpleDateFormat("d MMM");
String date = format.format(updatedate);
viewHolderForecast.dateTV.setText(date);
if(f.getForecast_text().equals("Moderate or heavy rain shower")){
viewHolderForecast.conditionTV.setText("Moderate/Heavy Showers");
}else{
viewHolderForecast.conditionTV.setText(f.getForecast_text());
}
viewHolderForecast.weatherIV.setImageResource(getImageResource(f.getIconURL(), context));
if (mSettings.getString("temp_units","").equals("°F")){
viewHolderForecast.mainTempTV.setText(changeToInt(f.getMaxtemp_f()));
viewHolderForecast.minorTempTV.setText(changeToInt(f.getMintemp_f()));
}else {
viewHolderForecast.mainTempTV.setText(changeToInt(f.getMaxtemp_c()));
viewHolderForecast.minorTempTV.setText(changeToInt(f.getMintemp_c()));
}
viewHolderForecast.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openFurtherInfo(f);
}
});
break;
case 3:
final ViewHolderFurtherDetails viewHolderFurtherDetails = (ViewHolderFurtherDetails) holder;
if (mSettings.getString("wind_units","").equals("mph")) {
viewHolderFurtherDetails.windSpeed.setText(new StringBuilder().append(changeToInt(currentForecast.getWind_mph()))
.append(context.getResources().getStringArray(R.array.list_preference_wind_values)[0]));
}else{
viewHolderFurtherDetails.windSpeed.setText(new StringBuilder().append(changeToInt(currentForecast.getWind_kph()))
.append(context.getResources().getStringArray(R.array.list_preference_wind_values)[1]).toString());
}
viewHolderFurtherDetails.windDirection.setText(currentForecast.getWind_dir());
viewHolderFurtherDetails.humidity.setText(new StringBuilder().append(changeToInt(currentForecast.getHumidity()))
.append("%").toString());
if (mSettings.getString("precip_units","").equals("in")) {
viewHolderFurtherDetails.precipitation.setText(new StringBuilder().append(changeToInt(currentForecast.getPrecip_mm()))
.append(context.getResources().getStringArray(R.array.list_preference_precip_values)[1]));
}else{
viewHolderFurtherDetails.precipitation.setText(new StringBuilder().append(changeToInt(currentForecast.getPrecip_mm()))
.append(context.getResources().getStringArray(R.array.list_preference_precip_values)[0]));
}
viewHolderFurtherDetails.clouds.setText(changeToInt(currentForecast.getCloud()) + "%");
}
}
@Override
public int getItemCount() {
return forecastItem.getForecastArrayList().size() + 2;
}
@Override
public int getItemViewType(int position) {
int type = 0;
if (position == 0){
type = 1;
}else if (position >= 1 && position < getItemCount() -1){
type = 2;
}else if (position == getItemCount() -1){
type = 3;
}
return type;
}
private void openFurtherInfo(Forecast forcast){
Intent i = new Intent(context, FurtherInfoActivity.class);
i.putExtra("currentForcast",forcast);
context.startActivity(i);
}
}

View File

@@ -1,207 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.ui.home;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.Loader;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.appttude.h_mal.atlas_weather.R;
import com.appttude.h_mal.atlas_weather.legacy.ForecastLoader;
import com.appttude.h_mal.atlas_weather.legacy.model.ForecastItem;
import com.appttude.h_mal.atlas_weather.legacy.services.location.getLatLong;
import com.appttude.h_mal.atlas_weather.legacy.ui.adapters.RecyclerViewAdapter;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import static com.appttude.h_mal.atlas_weather.legacy.data.network.RetrieveJSON.UriBuilder;
import static com.appttude.h_mal.atlas_weather.legacy.data.network.RetrieveJSON.createUrl;
import static com.appttude.h_mal.atlas_weather.legacy.services.location.getLatLong.latitude;
import static com.appttude.h_mal.atlas_weather.legacy.services.location.getLatLong.longitude;
public class FragmentHome extends Fragment implements LoaderManager.LoaderCallbacks<List<ForecastItem>>{
protected static String APIkey = "1fe09c8cd3c42e573c5cc7c32b27a1b4";
LoaderManager loaderManager;
LoaderManager.LoaderCallbacks callbacks;
public static int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
private static final int NEWS_LOADER_ID = 1;
RecyclerView forecastsListView;
LinearLayout emptyList;
ProgressBar mainPG;
Button button;
private RecyclerViewAdapter mAdapter;
SwipeRefreshLayout mSwipeRefreshLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
if (ActivityCompat.checkSelfPermission(getContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
android.Manifest.permission.ACCESS_FINE_LOCATION)) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
}
forecastsListView = rootView.findViewById(R.id.forecast_listview);
forecastsListView.setLayoutManager(new LinearLayoutManager(getContext()));
emptyList = rootView.findViewById(R.id.emptyView);
mainPG = rootView.findViewById(R.id.mainPB);
emptyList.setVisibility(View.GONE);
button = emptyList.findViewById(R.id.emptyViewButton);
mSwipeRefreshLayout = rootView.findViewById(R.id.swipe_refresh);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
loaderManager.restartLoader(NEWS_LOADER_ID,null,callbacks);
mSwipeRefreshLayout.setRefreshing(true);
}
});
loaderManager = getLoaderManager();
callbacks = this;
// if (MainActivity.networkInfo != null && MainActivity.networkInfo.isConnected()) {
loaderManager.initLoader(NEWS_LOADER_ID, null, callbacks);
// } else {
// emptyList.setVisibility(View.VISIBLE);
// mainPG.setVisibility(View.GONE);
// button.setVisibility(View.GONE);
// }
return rootView;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION){
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
try {
getLatLong.configLatLong(getContext());
}catch (Exception e){
System.out.println("error msg: " + e);
}
loaderManager.restartLoader(NEWS_LOADER_ID, null, callbacks);
Toast.makeText(getContext(), "Permission granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getContext(), "Permission denied", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public Loader<List<ForecastItem>> onCreateLoader(int id, Bundle args) {
mainPG.setVisibility(View.VISIBLE);
ArrayList<String> entries = new ArrayList<>();
try {
getLatLong.configLatLong(getContext());
}catch (Exception e){
System.out.println("error msg: " + e);
}finally {
if (latitude != null){
URL url = createUrl(UriBuilder(7));
entries.add(url.toString());
}
}
// getLatLong.configLatLong(getContext());
//// Toast.makeText(getContext(), "" + getLatLong.latitude.toString(), Toast.LENGTH_SHORT).show();
// URL url = createUrl(UriBuilder());
// ArrayList<String> entries = new ArrayList<>();
// entries.add(url.toString());
return new ForecastLoader(getContext(),entries);
}
@Override
public void onLoadFinished(Loader<List<ForecastItem>> loader, List<ForecastItem> data) {
if (mSwipeRefreshLayout.isRefreshing()){
mSwipeRefreshLayout.setRefreshing(false);
}
if (ActivityCompat.checkSelfPermission(getContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED){
emptyList.setVisibility(View.VISIBLE);
TextView tv = emptyList.findViewById(R.id.emptyViewText);
tv.setText("Location Required");
button.setVisibility(View.VISIBLE);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
grantPermission();
}
});
} else {
if (data == null){
forecastsListView.setVisibility(View.GONE);
emptyList.setVisibility(View.VISIBLE);
button.setVisibility(View.GONE);
Toast.makeText(getContext(), "Could not retrieve data", Toast.LENGTH_SHORT).show();
}else{
mAdapter = new RecyclerViewAdapter(getContext(),data.get(0),latitude,longitude);
mAdapter.notifyDataSetChanged();
forecastsListView.setAdapter(mAdapter);
forecastsListView.setVisibility(View.VISIBLE);
emptyList.setVisibility(View.GONE);
button.setVisibility(View.GONE);
}
}
mainPG.setVisibility(View.GONE);
}
@Override
public void onLoaderReset(Loader<List<ForecastItem>> loader) {
loader.reset();
}
public void grantPermission(){
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
loaderManager.restartLoader(NEWS_LOADER_ID, null, callbacks);
}
}

View File

@@ -1,167 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.ui.home;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import androidx.fragment.app.Fragment;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.Loader;
import com.appttude.h_mal.atlas_weather.R;
import com.appttude.h_mal.atlas_weather.legacy.ForecastLoader;
import com.appttude.h_mal.atlas_weather.legacy.data.sql.ForecastContract;
import com.appttude.h_mal.atlas_weather.legacy.data.sql.ForecastContract.ForecastEntry;
import com.appttude.h_mal.atlas_weather.legacy.model.ForecastItem;
import com.appttude.h_mal.atlas_weather.legacy.ui.AddForecastActivity;
import com.appttude.h_mal.atlas_weather.legacy.ui.WorldItemActivity;
import com.appttude.h_mal.atlas_weather.legacy.ui.adapters.CurrentForecastAdapter;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.ArrayList;
import java.util.List;
import static com.appttude.h_mal.atlas_weather.legacy.data.network.RetrieveJSON.UriBuilder;
import static com.appttude.h_mal.atlas_weather.legacy.ui.home.MainActivity.networkInfo;
public class FragmentTwo extends Fragment implements LoaderManager.LoaderCallbacks<List<ForecastItem>>{
private static final int NEWS_LOADER_ID = 1;
private String TAG = getClass().getSimpleName();
CurrentForecastAdapter mAdapter;
ProgressBar pb_2;
LinearLayout emptyView;
ListView lv;
LoaderManager loaderManager;
public FragmentTwo() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment__two, container, false);
emptyView = rootView.findViewById(R.id.emptyView);
pb_2 = rootView.findViewById(R.id.progressBar2);
lv = rootView.findViewById(R.id.listview);
final LoaderManager.LoaderCallbacks callbacks = this;
loaderManager = getLoaderManager();
loaderManager.initLoader(NEWS_LOADER_ID, null, callbacks);
mAdapter = new CurrentForecastAdapter(getActivity(), new ArrayList<ForecastItem>());
lv.setAdapter(mAdapter);
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int i, long l) {
new AlertDialog.Builder(getContext())
.setTitle("Delete?")
.setMessage("Continue?")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Cursor cursor = getActivity().getContentResolver().query(ForecastContract.ForecastEntry.CONTENT_URI,
new String[]{ForecastContract.ForecastEntry._ID,ForecastContract.ForecastEntry.COLUMN_FORECAST_NAME},
null,null,null);
cursor.moveToPosition(i);
int id = cursor.getInt(cursor.getColumnIndexOrThrow(ForecastContract.ForecastEntry._ID));
String selection = ForecastContract.ForecastEntry._ID + "=?";
getActivity().getContentResolver().delete(ForecastContract.ForecastEntry.CONTENT_URI,
selection, new String[]{id + ""});
loaderManager.restartLoader(NEWS_LOADER_ID, null, callbacks);
}
}).create().show();
return true;
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getContext(), WorldItemActivity.class);
intent.putExtra("ForecastItem",mAdapter.getItem(i));
startActivity(intent);
}
});
FloatingActionButton fab = rootView.findViewById(R.id.floatingActionButton);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getActivity(), AddForecastActivity.class);
startActivity(i);
}
});
if (networkInfo != null && networkInfo.isConnected()) {
loaderManager.initLoader(NEWS_LOADER_ID, null, callbacks);
} else {
// emptyList.setVisibility(View.VISIBLE);
// mainPG.setVisibility(View.GONE);
}
return rootView;
}
@Override
public void onResume() {
super.onResume();
loaderManager.restartLoader(NEWS_LOADER_ID, null, this);
}
@Override
public Loader<List<ForecastItem>> onCreateLoader(int id, Bundle args) {
pb_2.setVisibility(View.VISIBLE);
lv.setVisibility(View.GONE);
ArrayList<String> entries = new ArrayList<>();
String[] projection = {ForecastEntry.COLUMN_FORECAST_NAME};
Cursor cursor = getActivity().getContentResolver().query(ForecastEntry.CONTENT_URI,
projection,null,null,null);
try {
while (cursor.moveToNext()) {
final String descriptionColumnIndex = cursor.getString(cursor.getColumnIndexOrThrow(ForecastEntry.COLUMN_FORECAST_NAME));
entries.add(UriBuilder(descriptionColumnIndex));
}
}catch (Exception e){
Log.e(TAG, "onCreateLoader: ",e );
}finally {
if (cursor != null) {
cursor.close();
}
}
return new ForecastLoader(getContext(),entries);
}
@Override
public void onLoadFinished(Loader<List<ForecastItem>> loader, List<ForecastItem> data) {
mAdapter.clear();
}
@Override
public void onLoaderReset(Loader<List<ForecastItem>> loader) {
pb_2.setVisibility(View.GONE);
mAdapter.clear();
}
}

View File

@@ -1,227 +0,0 @@
package com.appttude.h_mal.atlas_weather.legacy.ui.home;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Address;
import android.location.Geocoder;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.appttude.h_mal.atlas_weather.R;
import com.appttude.h_mal.atlas_weather.legacy.data.sql.ForecastDBHelper;
import com.appttude.h_mal.atlas_weather.legacy.services.notifcation.NotificationReceiver;
import com.appttude.h_mal.atlas_weather.legacy.ui.InfoActivity;
import com.appttude.h_mal.atlas_weather.legacy.ui.UnitSettingsActivity;
import com.google.android.material.tabs.TabLayout;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private TabLayout tabLayout;
private ViewPager mViewPager;
public ForecastDBHelper forecastsDbhelper;
private String TAG = getClass().getSimpleName();
public static NetworkInfo networkInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
// iconsInTabs(tabLayout);
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean first_time_run = SP.getBoolean("FIRST_TIME_RUN",true);
Log.i(TAG, "onCreate: notification setup" + first_time_run);
if (first_time_run) {
setupNotificationBroadcaster(getApplicationContext());
// cal.add(Calendar.SECOND, 5);
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
// }
}
iconsInTabs(tabLayout);
ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
networkInfo = connectivityManager.getActiveNetworkInfo();
forecastsDbhelper = new ForecastDBHelper(getApplicationContext());
}
private void iconsInTabs (TabLayout tabLayout){
int[] tabIcons = {R.mipmap.ic_home,R.mipmap.ic_world};
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id){
case R.id.action_settings:
Intent i = new Intent(this, UnitSettingsActivity.class);
startActivity(i);
return true;
case R.id.action_into:
Intent infoActivity = new Intent(this, InfoActivity.class);
startActivity(infoActivity);
return true;
}
return super.onOptionsItemSelected(item);
}
public static String changeToInt(Double d){
return String.valueOf(Math.round(d));
}
public static int getImageResource(String s, Context context){
s =s.replace("cdn.apixu.com/weather/64x64/","");
s = s.replace(".png","");
s =s.replace("/","_");
s = s.substring(2);
return context.getResources().getIdentifier(s,"drawable",context.getPackageName());
}
public static String getLocationName(Context context, Double latitude, Double longitude) {
Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
String result = "";
List<Address> list = null;
try {
list = geoCoder.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (list != null & list.size() > 0) {
Address address = list.get(0);
result = address.getLocality();
}
}
return result;
}
public static void setupNotificationBroadcaster(Context context){
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(context, NotificationReceiver.class);
PendingIntent broadcast = PendingIntent.getBroadcast(context, 100, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 6);
cal.set(Calendar.MINUTE, 8);
cal.set(Calendar.SECOND, 5);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, broadcast);
}
/**
* A placeholder fragment containing a simple view.
*/
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs_menu/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
FragmentHome tab1 = new FragmentHome();
return tab1;
case 1:
FragmentTwo tab2 = new FragmentTwo();
return tab2;
default:
return null;
}
}
@Override
public int getCount() {
// Show 2 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
// switch (position) {
// case 0:
// return "Home";
// case 1:
// return "World";
// }
return null;
}
}
}

View File

@@ -8,8 +8,10 @@ import com.appttude.h_mal.atlas_weather.mvvm.data.network.interceptors.NetworkCo
import com.appttude.h_mal.atlas_weather.mvvm.data.network.interceptors.QueryParamsInterceptor
import com.appttude.h_mal.atlas_weather.mvvm.data.prefs.PreferenceProvider
import com.appttude.h_mal.atlas_weather.mvvm.data.repository.RepositoryImpl
import com.appttude.h_mal.atlas_weather.mvvm.data.repository.SettingsRepositoryImpl
import com.appttude.h_mal.atlas_weather.mvvm.data.room.AppDatabase
import com.appttude.h_mal.atlas_weather.mvvm.viewmodel.ApplicationViewModelFactory
import com.appttude.h_mal.atlas_weather.notifcation.ServicesHelper
import com.google.gson.Gson
import org.kodein.di.Kodein
import org.kodein.di.KodeinAware
@@ -33,13 +35,16 @@ class AppClass : Application(), KodeinAware {
import(androidXModule(this@AppClass))
bind() from singleton { Gson() }
bind() from singleton { NetworkConnectionInterceptor(instance()) }
bind() from singleton { QueryParamsInterceptor() }
bind() from singleton { WeatherApi(instance(), instance())}
bind() from singleton { AppDatabase(instance()) }
bind() from singleton { PreferenceProvider(instance()) }
bind() from singleton { RepositoryImpl(instance(), instance(), instance()) }
bind() from singleton { SettingsRepositoryImpl(instance()) }
bind() from singleton { LocationProvider(instance()) }
bind() from singleton { ServicesHelper(instance(), instance(), instance()) }
bind() from provider { ApplicationViewModelFactory(instance(), instance()) }
}

View File

@@ -37,4 +37,10 @@ class PreferenceProvider(
preference.edit().remove(locationName).apply()
}
fun isNotificationsEnabled(): Boolean = preference.getBoolean("notif_boolean", true)
fun setFirstTimeRun(){
preference.edit().putBoolean("FIRST_TIME_RUN", false).apply()
}
}

View File

@@ -11,6 +11,7 @@ interface Repository {
suspend fun saveWeatherListToRoom(list: List<EntityItem>)
fun loadAllWeatherExceptCurrentFromRoom(): LiveData<List<EntityItem>>
fun loadCurrentWeatherFromRoom(id: String): LiveData<EntityItem>
suspend fun loadSingleCurrentWeatherFromRoom(id: String): EntityItem
fun isSearchValid(locationName: String): Boolean
fun saveLastSavedAt(locationName: String)
suspend fun deleteSavedWeatherEntry(locationName: String)

View File

@@ -44,6 +44,8 @@ class RepositoryImpl(
override fun loadCurrentWeatherFromRoom(id: String) = db.getSimpleDao().getCurrentFullWeather(id)
override suspend fun loadSingleCurrentWeatherFromRoom(id: String) = db.getSimpleDao().getCurrentFullWeatherSingle(id)
override fun isSearchValid(locationName: String): Boolean {
val lastSaved = prefs.getLastSavedAt(locationName) ?: return true
val difference = System.currentTimeMillis() - lastSaved

View File

@@ -0,0 +1,6 @@
package com.appttude.h_mal.atlas_weather.mvvm.data.repository
interface SettingsRepository {
fun isNotificationsEnabled(): Boolean
fun setFirstTime()
}

View File

@@ -0,0 +1,12 @@
package com.appttude.h_mal.atlas_weather.mvvm.data.repository
import com.appttude.h_mal.atlas_weather.mvvm.data.prefs.PreferenceProvider
class SettingsRepositoryImpl(
val prefs: PreferenceProvider
) : SettingsRepository{
override fun isNotificationsEnabled(): Boolean = prefs.isNotificationsEnabled()
override fun setFirstTime() = prefs.setFirstTimeRun()
}

View File

@@ -20,6 +20,9 @@ interface WeatherDao {
@Query("SELECT * FROM EntityItem WHERE id = :userId LIMIT 1")
fun getCurrentFullWeather(userId: String) : LiveData<EntityItem>
@Query("SELECT * FROM EntityItem WHERE id = :userId LIMIT 1")
suspend fun getCurrentFullWeatherSingle(userId: String) : EntityItem
@Query("SELECT * FROM EntityItem WHERE id != :id")
fun getAllFullWeatherWithoutCurrent(id: String = CURRENT_LOCATION) : LiveData<List<EntityItem>>

View File

@@ -9,7 +9,7 @@ data class FullWeather(
val daily: List<DailyWeather>? = null,
val lon: Double = 0.00,
val lat: Double = 0.00
){
) {
constructor(weatherResponse: WeatherResponse): this(
weatherResponse.current?.let { Current(it) },
@@ -19,6 +19,7 @@ data class FullWeather(
weatherResponse.lon,
weatherResponse.lat
)
}

View File

@@ -10,9 +10,8 @@ import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.appttude.h_mal.atlas_weather.R
import com.appttude.h_mal.atlas_weather.legacy.ui.InfoActivity
import com.appttude.h_mal.atlas_weather.legacy.ui.UnitSettingsActivity
import com.appttude.h_mal.atlas_weather.mvvm.ui.home.BaseActivity
import com.appttude.h_mal.atlas_weather.mvvm.ui.settings.UnitSettingsActivity
import com.google.android.material.bottomnavigation.BottomNavigationView
import kotlinx.android.synthetic.main.activity_main_navigation.*
@@ -59,11 +58,6 @@ class MainActivity : BaseActivity() {
startActivity(i)
return true
}
R.id.action_into -> {
val infoActivity = Intent(this, InfoActivity::class.java)
startActivity(infoActivity)
return true
}
}
return super.onOptionsItemSelected(item)
}

View File

@@ -0,0 +1,80 @@
package com.appttude.h_mal.atlas_weather.mvvm.ui.settings
import android.app.AlarmManager
import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import android.os.Bundle
import android.preference.PreferenceActivity
import android.preference.PreferenceFragment
import android.preference.PreferenceManager
import com.appttude.h_mal.atlas_weather.R
import com.appttude.h_mal.atlas_weather.notifcation.NotificationReceiver
import com.appttude.h_mal.atlas_weather.widget.NewAppWidget
import java.util.*
class UnitSettingsActivity : PreferenceActivity() {
private val TAG = javaClass.simpleName
private var prefListener: OnSharedPreferenceChangeListener? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
PreferenceManager.setDefaultValues(this, R.xml.prefs, false)
fragmentManager.beginTransaction().replace(android.R.id.content, MyPreferenceFragment()).commit()
//listener on changed sort order preference:
val prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext)
prefListener = OnSharedPreferenceChangeListener { prefs, key ->
if (key == "temp_units") {
val intent = Intent(baseContext, NewAppWidget::class.java)
intent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
val ids = AppWidgetManager.getInstance(application).getAppWidgetIds(ComponentName(application, NewAppWidget::class.java))
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
sendBroadcast(intent)
}
if (key == "notif_boolean") {
setupNotificationBroadcaster(baseContext)
}
}
prefs.registerOnSharedPreferenceChangeListener(prefListener)
}
fun setupNotificationBroadcaster(context: Context) {
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val notificationIntent = Intent(context, NotificationReceiver::class.java)
val broadcast = PendingIntent.getBroadcast(context, 100, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT)
val cal: Calendar = Calendar.getInstance()
cal.set(Calendar.HOUR_OF_DAY, 6)
cal.set(Calendar.MINUTE, 8)
cal.set(Calendar.SECOND, 5)
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, broadcast)
}
override fun onBackPressed() {
super.onBackPressed()
}
// @Override
// public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
// Log.i(TAG, "onSharedPreferenceChanged: " + s);
// if (s == "temp_units"){
// Intent intent = new Intent(getBaseContext(), NewAppWidget.class);
// intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
//
// int[] ids = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), NewAppWidget.class));
// intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
// sendBroadcast(intent);
// }
// }
class MyPreferenceFragment : PreferenceFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
addPreferencesFromResource(R.xml.prefs)
}
}
}

View File

@@ -32,6 +32,17 @@ fun Int.toDayName(): String {
}
}
fun Int.toSmallDayName(): String {
return try {
val date = Date(this.makeMilliseconds())
val format = SimpleDateFormat("EEE", Locale.getDefault())
format.format(date)
} catch (e: Exception) {
e.printStackTrace()
"Unable to parse date"
}
}
fun String.changeDateFormat(): String {
return try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

View File

@@ -0,0 +1,8 @@
package com.appttude.h_mal.atlas_weather.notifcation
import android.graphics.Bitmap
data class NotificationData(
val temp: String,
val icon: Bitmap
)

View File

@@ -0,0 +1,132 @@
package com.appttude.h_mal.atlas_weather.notifcation
import android.Manifest
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.annotation.RequiresPermission
import com.appttude.h_mal.atlas_weather.mvvm.data.location.LocationProvider
import com.appttude.h_mal.atlas_weather.mvvm.data.repository.Repository
import com.appttude.h_mal.atlas_weather.mvvm.data.repository.SettingsRepository
import com.appttude.h_mal.atlas_weather.mvvm.data.room.entity.CURRENT_LOCATION
import com.appttude.h_mal.atlas_weather.mvvm.model.weather.FullWeather
import com.appttude.h_mal.atlas_weather.mvvm.utils.toDayName
import com.appttude.h_mal.atlas_weather.mvvm.utils.toSmallDayName
import java.io.IOException
import java.net.URL
class ServicesHelper(
private val repository: Repository,
private val settingsRepository: SettingsRepository,
private val locationProvider: LocationProvider
) {
@RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION)
suspend fun getData(): FullWeather? {
return try {
val latLon = locationProvider.getLatLong()
val result =
repository.getWeatherFromApi(
latLon.first.toString(),
latLon.second.toString()
)
FullWeather(result)
} catch (e: Exception) {
null
}
}
@RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION)
suspend fun fetchData(): Boolean {
if (!repository.isSearchValid(CURRENT_LOCATION)) return true
return try {
// Get location
val latLong = locationProvider.getLatLong()
// Get weather from api
val weather = repository
.getWeatherFromApi(latLong.first.toString(), latLong.second.toString())
// Save data if not null
weather.let {
repository.saveLastSavedAt(CURRENT_LOCATION)
repository.saveCurrentWeatherToRoom(CURRENT_LOCATION, it)
}
false
} catch (e: IOException) {
false
}
}
suspend fun getWidgetWeather(): WidgetData? {
return try {
val result = repository.loadSingleCurrentWeatherFromRoom(CURRENT_LOCATION)
result.weather.let {
WidgetData(
locationProvider.getLocationName(it.lat, it.lon),
getBitmapFromUrl(it.daily?.get(0)?.icon),
it.current?.temp?.toInt().toString()
)
}
} catch (e: Exception) { null }
}
suspend fun getWidgetInnerWeather(): List<InnerWidgetData>? {
return try {
val result = repository.loadSingleCurrentWeatherFromRoom(CURRENT_LOCATION)
result.weather.daily?.drop(1)?.dropLast(2)?.map{
InnerWidgetData(
it.dt?.toSmallDayName(),
getBitmapFromUrl(it.icon),
it.max?.toInt().toString()
)
}
} catch (e: Exception) { null }
}
@RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION)
suspend fun getWidgetData(): WidgetData? {
return try {
val latLon = locationProvider.getLatLong()
val result =
repository.getWeatherFromApi(
latLon.first.toString(),
latLon.second.toString()
)
val list = result.daily?.drop(0)?.map {
InnerWidgetData(
it.dt?.toDayName(),
getBitmapFromUrl(it.weather?.get(0)?.icon),
it.temp?.max?.toInt().toString()
)
}
WidgetData(
locationProvider.getLocationName(latLon.first, latLon.second),
getBitmapFromUrl(result.current?.weather?.get(0)?.icon),
result.current?.temp?.toInt().toString(),
list
)
} catch (e: Exception) {
null
}
}
fun getBitmapFromUrl(imageAddress: String?): Bitmap? {
return try {
val url = URL(imageAddress)
BitmapFactory.decodeStream(url.openConnection().getInputStream())
} catch (e: IOException) {
e.printStackTrace()
null
}
}
fun isEnabled() = settingsRepository.isNotificationsEnabled()
fun setFirstTimer() = settingsRepository.setFirstTime()
}

View File

@@ -0,0 +1,78 @@
package com.appttude.h_mal.atlas_weather.notifcation
import android.graphics.Bitmap
import android.os.Parcel
import android.os.Parcelable
data class WidgetData(
val location: String?,
val icon: Bitmap?,
val currentTemp: String?,
val list: List<InnerWidgetData>? = listOf()
):Parcelable{
constructor(parcel: Parcel) : this(
parcel.readString(),
parcel.readParcelable(Bitmap::class.java.classLoader),
parcel.readString(),
parcel.createTypedArrayList(InnerWidgetData)
)
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(location)
parcel.writeParcelable(icon, flags)
parcel.writeString(currentTemp)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<WidgetData> {
override fun createFromParcel(parcel: Parcel): WidgetData {
return WidgetData(parcel)
}
override fun newArray(size: Int): Array<WidgetData?> {
return arrayOfNulls(size)
}
}
}
data class InnerWidgetData(
val date: String?,
val icon: Bitmap?,
val currentTemp: String?
):Parcelable{
constructor(parcel: Parcel) : this(
parcel.readString(),
parcel.readParcelable(Bitmap::class.java.classLoader),
parcel.readString()
)
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(date)
parcel.writeParcelable(icon, flags)
parcel.writeString(currentTemp)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<InnerWidgetData> {
override fun createFromParcel(parcel: Parcel): InnerWidgetData {
return InnerWidgetData(parcel)
}
override fun newArray(size: Int): Array<InnerWidgetData?> {
return arrayOfNulls(size)
}
}
}
interface WidgetDataImplementation{
fun getWidgetData(): WidgetData
}

View File

@@ -0,0 +1,37 @@
package com.appttude.h_mal.atlas_weather.widget
import android.app.Activity
import android.app.PendingIntent
import android.app.TaskStackBuilder
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.Context
import android.content.Intent
import android.widget.RemoteViews
import androidx.annotation.LayoutRes
abstract class BaseWidgetClass : AppWidgetProvider(){
fun createRemoteView(context: Context, @LayoutRes id: Int): RemoteViews {
return RemoteViews(context.packageName, id)
}
fun createUpdatePendingIntent(context: Context, appWidgetId: Int): PendingIntent? {
val seconds = (System.currentTimeMillis() / 1000L).toInt()
val intentUpdate = Intent(context, this::class.java)
intentUpdate.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
val idArray = intArrayOf(appWidgetId)
intentUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, idArray)
return PendingIntent.getBroadcast(
context, seconds, intentUpdate,
PendingIntent.FLAG_UPDATE_CURRENT)
}
fun <T: Activity> createClickingPendingIntent(context: Context, activityClass: Class<T>): PendingIntent {
val clickIntentTemplate = Intent(context, activityClass)
return TaskStackBuilder.create(context)
.addNextIntentWithParentStack(clickIntentTemplate)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
}
}

View File

@@ -23,21 +23,20 @@
android:layout_marginLeft="12dp"
android:orientation="vertical">
<TextClock
android:id="@+id/textclock_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoSizeMaxTextSize="100sp"
android:autoSizeMinTextSize="12sp"
android:autoSizeStepGranularity="2sp"
android:autoSizeTextType="uniform"
android:format12Hour="h:mm a"
android:includeFontPadding="false"
android:maxLines="1"
android:textColor="#ffffff"
android:textSize="32sp"
tools:text="4:22 AM" />
<!-- <TextClock-->
<!-- android:id="@+id/textclock_time"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:autoSizeMaxTextSize="100sp"-->
<!-- android:autoSizeMinTextSize="12sp"-->
<!-- android:autoSizeStepGranularity="2sp"-->
<!-- android:autoSizeTextType="uniform"-->
<!-- android:format12Hour="h:mm a"-->
<!-- android:includeFontPadding="false"-->
<!-- android:maxLines="1"-->
<!-- android:textColor="#ffffff"-->
<!-- android:textSize="32sp"-->
<!-- tools:text="4:22 AM" />-->
<TextClock
android:id="@+id/clock_date"
android:layout_width="wrap_content"
@@ -158,38 +157,7 @@
tools:text="°C" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right">
<TextView
android:id="@+id/widget_item_temp_high"
style="@style/textView_minor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:autoSizeMaxTextSize="100sp"
android:autoSizeMinTextSize="12sp"
android:autoSizeStepGranularity="2sp"
android:autoSizeTextType="uniform"
tools:text="20°" />
<TextView
android:id="@+id/dash"
style="@style/textView_minor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text=" / " />
<TextView
android:id="@+id/widget_item_temp_low"
style="@style/textView_minor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
tools:text="19°" />
</LinearLayout>
</LinearLayout>

View File

@@ -25,41 +25,16 @@
android:adjustViewBounds="true"
tools:src="@drawable/demo_icon" />
<LinearLayout
<TextView
android:id="@+id/widget_item_temp_high"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center">
<TextView
android:id="@+id/widget_item_temp_high"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:includeFontPadding="false"
android:textColor="#ffffff"
android:textSize="12sp"
tools:text="20°" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:includeFontPadding="false"
android:text=" / "
android:textColor="#ffffff"
android:textSize="12sp" />
<TextView
android:id="@+id/widget_item_temp_low"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:includeFontPadding="false"
android:textColor="#ffffff"
android:textSize="12sp"
tools:text="19°" />
</LinearLayout>
android:includeFontPadding="false"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="12sp"
tools:text="20" />
</LinearLayout>

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_item_layout"
android:layout_width="match_parent"
android:layout_height="82dp"
android:orientation="vertical">
<TextView
android:id="@+id/widget_item_day"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:includeFontPadding="false"
android:textColor="#ffffff"
android:text="Loading \nforecast..." />
</LinearLayout>

View File

@@ -6,10 +6,5 @@
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
<item
android:id="@+id/action_into"
android:orderInCategory="100"
android:title="@string/action_info"
app:showAsAction="never" />
app:showAsAction="ifRoom" />
</menu>