Skip to content

Commit

Permalink
Add Sync Adapter
Browse files Browse the repository at this point in the history
Test Service first and later add sync adapter to
update weather data periodicaly. Display notifications and
preference to display them or not. Delete old data from database.
  • Loading branch information
Protino committed Aug 22, 2016
1 parent 0ad58fd commit 52eef07
Show file tree
Hide file tree
Showing 20 changed files with 848 additions and 175 deletions.

This file was deleted.

40 changes: 35 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
package="com.calgen.prodek.sunshine_v2">

<uses-permission android:name="android.permission.INTERNET" />
<!-- Permissions required by the sync adapter -->
<uses-permission
android:name="android.permission.READ_SYNC_SETTINGS"/>
<uses-permission
android:name="android.permission.WRITE_SYNC_SETTINGS"/>
<uses-permission
android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>

<application
android:allowBackup="true"
Expand All @@ -23,24 +30,47 @@
<activity
android:name=".activity.DetailActivity"
android:label="@string/title_activity_detail"
android:parentActivityName=".activity.MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.MainActivity" />
</activity>
<activity
android:name=".activity.SettingsActivity"
android:label="@string/title_activity_settings"
android:parentActivityName=".activity.MainActivity">
android:label="@string/title_activity_settings">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.MainActivity" />
</activity>

<!-- SyncAdapter's dummy authentication service -->
<service
android:name=".sync.SunshineAuthenticatorService"
>
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator" />
</intent-filter>
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator" />
</service>

<provider
android:authorities="@string/content_authority"
android:name=".data.WeatherProvider"
android:authorities="com.example.android.sunshine.app" />
</application>
android:exported="false"
android:syncable="true" />

<!-- The SyncAdapter service -->
<service
android:name=".sync.SunshineSyncService"
android:exported="true">
<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>
<meta-data
android:name="android.content.SyncAdapter"
android:resource="@xml/syncadapter" />
</service>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,27 @@
import android.view.MenuItem;

import com.calgen.prodek.sunshine_v2.R;
import com.calgen.prodek.sunshine_v2.Utility;
import com.calgen.prodek.sunshine_v2.fragment.DetailFragment;

public class DetailActivity extends AppCompatActivity {

private static final String TAG = DetailActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Weather Detail");
if (savedInstanceState == null) {

Bundle arguments = new Bundle();
arguments.putParcelable(DetailFragment.DETAIL_URI, getIntent().getData());

String date = (getIntent().getData()).getLastPathSegment();
date = Utility.getFormattedMonthDay(this,Long.parseLong(date));
getWindow().getDecorView().setContentDescription("Weather details for "+date);
DetailFragment fragment = new DetailFragment();
fragment.setArguments(arguments);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.calgen.prodek.sunshine_v2.Utility;
import com.calgen.prodek.sunshine_v2.fragment.DetailFragment;
import com.calgen.prodek.sunshine_v2.fragment.ForecastFragment;
import com.calgen.prodek.sunshine_v2.sync.SunshineSyncAdapter;

public class MainActivity extends AppCompatActivity implements ForecastFragment.Callback {

Expand Down Expand Up @@ -43,9 +44,11 @@ protected void onCreate(Bundle savedInstanceState) {
mTwoPane = false;
}

ForecastFragment forecastFragment= (ForecastFragment)(getSupportFragmentManager().findFragmentById(R.id.fragment_forecast));
ForecastFragment forecastFragment = (ForecastFragment) (getSupportFragmentManager().findFragmentById(R.id.fragment_forecast));
forecastFragment.setUseTodayLayout(!mTwoPane);

SunshineSyncAdapter.initializeSyncAdapter(this);

}

@Override
Expand Down Expand Up @@ -132,5 +135,4 @@ public void onItemSelected(Uri dateUri) {
}
}


}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.calgen.prodek.sunshine_v2.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceManager;
Expand All @@ -17,6 +15,8 @@
*/
public class SettingsActivity extends AppCompactPreferenceActivity implements Preference.OnPreferenceChangeListener {

private static final String TAG = SettingsActivity.class.getSimpleName();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -29,6 +29,8 @@ public void onCreate(Bundle savedInstanceState) {
// updated when the preference changes.
bindPreferenceSummaryToValue(findPreference(getResources().getString(R.string.pref_location_key)));
bindPreferenceSummaryToValue(findPreference(getResources().getString(R.string.pref_temperature_key)));
CheckBoxPreference notificationPreference = (CheckBoxPreference) findPreference(getResources().getString(R.string.pref_notification_key));
notificationPreference.setOnPreferenceChangeListener(this);
}


Expand Down Expand Up @@ -73,14 +75,9 @@ public boolean onPreferenceChange(Preference preference, Object value) {
}
} else {
// For other preferences, set the summary to the value's simple string representation.
preference.setSummary(stringValue);
if (!(preference instanceof CheckBoxPreference))
preference.setSummary(stringValue);
}
return true;
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public Intent getParentActivityIntent() {
return super.getParentActivityIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public long addLocation(String locationSetting, String cityName, double lat, dou
* Fortunately parsing is easy: constructor takes the JSON string and converts it
* into an Object hierarchy for us.
*/
private void getWeatherDataFromJson(String forecastJsonStr,
String locationSetting)
public void getWeatherDataFromJson(String forecastJsonStr,
String locationSetting)
throws JSONException {

// Now we have a String representing the complete forecast in JSON Format.
Expand Down Expand Up @@ -224,7 +224,6 @@ private void getWeatherDataFromJson(String forecastJsonStr,
Uri weatherForLocationUri = WeatherEntry.buildWeatherLocationWithStartDate(
locationSetting, System.currentTimeMillis());

// Students: Uncomment the next lines to display what what you stored in the bulkInsert

Cursor cur = mContext.getContentResolver().query(weatherForLocationUri,
null, null, null, sortOrder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class DetailFragment extends Fragment implements LoaderManager.LoaderCall
private TextView mHumidityView;
private TextView mWindView;
private TextView mPressureView;
View rootView;

public DetailFragment() {
setHasOptionsMenu(true);
Expand All @@ -83,7 +84,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
if (arguments != null) {
mUri = arguments.getParcelable(DetailFragment.DETAIL_URI);
}
View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
rootView = inflater.inflate(R.layout.fragment_detail, container, false);
mIconView = (ImageView) rootView.findViewById(R.id.detail_icon);
mDateView = (TextView) rootView.findViewById(R.id.detail_date_textview);
mFriendlyDateView = (TextView) rootView.findViewById(R.id.detail_day_textview);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
Expand All @@ -20,7 +17,6 @@
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.FetchWeatherTask;
import com.calgen.prodek.sunshine_v2.data.WeatherContract;

/**
Expand Down Expand Up @@ -75,7 +71,6 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
setHasOptionsMenu(true);
setRetainInstance(true);
super.onCreate(savedInstanceState);
}
Expand All @@ -88,31 +83,6 @@ public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.forecast_fragment, menu);
super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

switch (id) {

case R.id.action_refresh:
updateWeather();
return true;
}
return super.onOptionsItemSelected(item);
}


@Override
public void onStart() {
super.onStart();
updateWeather();
}

public void setUseTodayLayout(boolean useTodayLayout) {
mUseTodayLayout = useTodayLayout;
Expand Down Expand Up @@ -192,13 +162,7 @@ public void onLoaderReset(Loader<Cursor> loader) {
mForecastAdapter.swapCursor(null);
}

private void updateWeather() {
FetchWeatherTask fetchWeatherTask = new FetchWeatherTask(getActivity());
fetchWeatherTask.execute(Utility.getPreferredLocation(getContext()));
}

public void onLocationChanged() {
updateWeather();
getLoaderManager().restartLoader(MY_LOADER_ID, null, this);
}

Expand Down
Loading

0 comments on commit 52eef07

Please sign in to comment.