Skip to content

Commit

Permalink
Add error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Protino committed Nov 8, 2016
1 parent f6ec242 commit b5e5328
Show file tree
Hide file tree
Showing 12 changed files with 2,123 additions and 21 deletions.
10 changes: 2 additions & 8 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package="com.calgen.prodek.sunshine_v2">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!-- Permissions required by the sync adapter -->
<uses-permission
android:name="android.permission.READ_SYNC_SETTINGS"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public long addLocation(String locationSetting, String cityName, double lat, dou
Uri insertedUri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI, values);
locationId = ContentUris.parseId(insertedUri);
}
cursor.close();
return locationId;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/**
* A placeholder fragment containing a simple view.
*/
public class DetailActivityFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
public class DetailActivityFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {


// These indices are tied to DETAIL_COLUMNS. If DETAIL_COLUMNS changes, these
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.calgen.prodek.sunshine_v2.fragment;

import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
Expand All @@ -13,16 +15,21 @@
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.calgen.prodek.sunshine_v2.R;
import com.calgen.prodek.sunshine_v2.Utility;
import com.calgen.prodek.sunshine_v2.adapter.ForecastAdapter;
import com.calgen.prodek.sunshine_v2.data.WeatherContract;
import com.calgen.prodek.sunshine_v2.sync.SunshineSyncAdapter.LocationStatus;

import static com.calgen.prodek.sunshine_v2.sync.SunshineSyncAdapter.LOCATION_STATUS_SERVER_DOWN;
import static com.calgen.prodek.sunshine_v2.sync.SunshineSyncAdapter.LOCATION_STATUS_SERVER_INVALID;

/**
* A placeholder fragment containing a simple view.
*/
public class ForecastFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
public class ForecastFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>, SharedPreferences.OnSharedPreferenceChangeListener {

// These indices are tied to FORECAST_COLUMNS. If FORECAST_COLUMNS changes, these
// must change.
Expand Down Expand Up @@ -58,6 +65,7 @@ public class ForecastFragment extends Fragment implements LoaderManager.LoaderCa
private static int mPosition;
private ForecastAdapter mForecastAdapter;
private ListView mListView;
private TextView mEmptyView;
private boolean mUseTodayLayout;

public ForecastFragment() {
Expand Down Expand Up @@ -100,6 +108,7 @@ public View onCreateView(final LayoutInflater inflater, ViewGroup container,
mListView = (ListView) rootView.findViewById(R.id.listview_forecast);
mListView.setAdapter(mForecastAdapter);


//set click listeners on the list items
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
Expand Down Expand Up @@ -155,6 +164,30 @@ public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// to, do so now.
mListView.smoothScrollToPosition(mPosition);
}
updateEmptyView();
}

private void updateEmptyView() {
if (mForecastAdapter.getCount() == 0) {
mEmptyView = (TextView) getView().findViewById(R.id.empty_view);
if (mEmptyView != null) {
int message = R.string.empty_forecast_list;
@LocationStatus int status = Utility.getLocationStatus(getContext());
switch (status) {
case LOCATION_STATUS_SERVER_DOWN:
message = R.string.empty_forecast_list_server_down;
break;
case LOCATION_STATUS_SERVER_INVALID:
message = R.string.empty_forecast_list_server_error;
break;
default:
if (!Utility.isNetworkAvailable(getContext())) {
message = R.string.empty_forecast_list_no_network;
}
}
mEmptyView.setText(message);
}
}
}

@Override
Expand All @@ -166,6 +199,25 @@ public void onLocationChanged() {
getLoaderManager().restartLoader(MY_LOADER_ID, null, this);
}

@Override
public void onResume() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext());
sp.registerOnSharedPreferenceChangeListener(this);
super.onResume();
}

@Override
public void onPause() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext());
sp.unregisterOnSharedPreferenceChangeListener(this);
super.onPause();
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(getString(R.string.pref_location_status_key))) updateEmptyView();
}


public interface Callback {
/**
Expand Down
27 changes: 27 additions & 0 deletions app/src/main/java/com/calgen/prodek/sunshine_v2/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.preference.PreferenceManager;
import android.text.format.Time;

import com.calgen.prodek.sunshine_v2.sync.SunshineSyncAdapter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
Expand Down Expand Up @@ -279,4 +283,27 @@ public static int getArtResourceForWeatherCondition(int weatherId) {
return -1;
}

/**
* @param context {@link Context} to fetch defaultSharedPreferences
* @return True if connected to a network else false
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnectedOrConnecting();
}

/**
* @param context {@link Context} to fetch defaultSharedPreferences
* @return the location status integer type
*/
@SuppressWarnings("ResourceType")
public static
@SunshineSyncAdapter.LocationStatus
int getLocationStatus(Context context) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
return sp.getInt(context.getString(R.string.pref_location_status_key),
SunshineSyncAdapter.LOCATION_STATUS_UNKNOWN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.IntDef;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.text.format.Time;
Expand All @@ -39,6 +40,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Calendar;
Expand All @@ -48,6 +51,19 @@ public class SunshineSyncAdapter extends AbstractThreadedSyncAdapter {

public static final int SYNC_INTERVAL = 10800;
public static final int SYNC_FLEXTIME = SYNC_INTERVAL / 3;


@Retention(RetentionPolicy.SOURCE)
@IntDef({LOCATION_STATUS_OK, LOCATION_STATUS_SERVER_DOWN, LOCATION_STATUS_SERVER_INVALID, LOCATION_STATUS_UNKNOWN})
public @interface LocationStatus {
}

//Constants for location status
public static final int LOCATION_STATUS_OK = 0;
public static final int LOCATION_STATUS_SERVER_DOWN = 1;
public static final int LOCATION_STATUS_SERVER_INVALID = 2;
public static final int LOCATION_STATUS_UNKNOWN = 3;

private static final String[] NOTIFY_WEATHER_PROJECTION = new String[]{
WeatherContract.WeatherEntry.COLUMN_WEATHER_ID,
WeatherContract.WeatherEntry.COLUMN_DATE,
Expand Down Expand Up @@ -228,14 +244,18 @@ public void onPerformSync(Account account, Bundle extras, String authority, Cont

if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
setLocationStatus(getContext(), LOCATION_STATUS_SERVER_DOWN);
return;
}
forecastJsonStr = buffer.toString();
getWeatherDataFromJson(forecastJsonStr, locationQuery);
setLocationStatus(getContext(), LOCATION_STATUS_OK);
} catch (IOException e) {
Log.e(TAG, "Error ", e);
setLocationStatus(getContext(), LOCATION_STATUS_SERVER_DOWN);
} catch (JSONException e) {
Log.e(TAG, e.getMessage(), e);
setLocationStatus(getContext(), LOCATION_STATUS_SERVER_INVALID);
e.printStackTrace();
} finally {
if (urlConnection != null) {
Expand Down Expand Up @@ -519,4 +539,16 @@ private void notifyWeather() {
}
}
}

@SunshineSyncAdapter.LocationStatus
public int getLocationStatus() {
return LOCATION_STATUS_OK;
}

static private void setLocationStatus(Context c,@LocationStatus int status) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(c);
SharedPreferences.Editor spe = sp.edit();
spe.putInt(c.getString(R.string.pref_location_status_key),status);
spe.commit();
}
}
10 changes: 5 additions & 5 deletions app/src/main/res/drawable/touch_selector.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- State when a row is being pressed, but hasn't yet been activated (finger down) -->
<item android:state_pressed="true"
android:drawable="@color/grey" />
<item android:drawable="@color/grey"
android:state_pressed="true"/>

<!-- When the view is "activated". In SINGLE_CHOICE_MODE, it flags the active row
of a ListView -->
<item android:state_activated="true"
android:drawable="@color/sunshine_light_blue" />
<item android:drawable="@color/sunshine_light_blue"
android:state_activated="true"/>

<!-- Default, "just hangin' out" state. -->
<item android:drawable="@android:color/transparent" />
<item android:drawable="@android:color/transparent"/>
</selector>
20 changes: 15 additions & 5 deletions app/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.ForecastFragment">
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.ForecastFragment">

<ListView
android:id="@+id/listview_forecast"
style="@style/ForecastListStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null" />
android:divider="@null"/>

<TextView
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
/>
</FrameLayout>
8 changes: 8 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<string name="pref_location_key" translatable="false">location</string>
<string name="pref_location_default" translatable="false">Bagalkot</string>
<string name="pref_location_status_key">LOCATION_STATUS_KEY</string>


<!-- Temperature Settings -->
Expand Down Expand Up @@ -64,4 +65,11 @@
<string name="pref_notification_key">notification_key</string>
<string name="pref_notification_title">Notifications</string>
<string name="pref_notification_default">true</string>

<!-- Error messages-->
<string name="empty_forecast_list">No weather information available.</string>
<string name="empty_forecast_list_no_network">No weather information available. The network is not available to fetch weather data. </string>
<string name="empty_forecast_list_server_down">No weather information available. The server is not returning data.</string>
<string name="empty_forecast_list_server_error">No weather information available. The server is not returning valid data. Please check for an updated version of Sunshine.</string>

</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'com.android.tools.build:gradle:2.2.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
Loading

0 comments on commit b5e5328

Please sign in to comment.