diff --git a/README.md b/README.md index 57b5ebe63..1f013b270 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,10 @@ +# Archival Note +We are archiving this repository because we do not want learners to push personal development to the current repository. If you have any issues or suggestions to make, feel free to: +- Utilize the https://knowledge.udacity.com/ forum to seek help on content-specific issues. +- Submit a support ticket along with the link to your forked repository if (learners are) blocked for other reasons. Here are the links for the [retail consumers](https://udacity.zendesk.com/hc/en-us/requests/new) and [enterprise learners](https://udacityenterprise.zendesk.com/hc/en-us/requests/new?ticket_form_id=360000279131). + + + Sunshine ======== @@ -7,10 +14,22 @@ Take the course to find out how to build this app a step at a time, and eventual This is the second version of the Sunshine code. The repository has been updated on: +* **October 18th, 2015** - Updated to support use of the openweathermap.org API key. * **February 13th, 2015** - Major update * February 25, 2015 - Minor bug fixes * March 4th, 2015 - Minor bug fixes +### Open Weather Map API Key is required. + +In order for the Sunshine app to function properly as of October 18th, 2015 an API key for openweathermap.org must be included with the build. + +We recommend that each student obtain a key via the following [instructions](http://openweathermap.org/appid#use), and include the unique key for the build by adding the following line to [USER_HOME]/.gradle/gradle.properties + +`MyOpenWeatherMapApiKey="` + +For help migrating an existing repo (fork or clone prior to 10/18/15), please check out this [guide.](https://docs.google.com/document/d/1e8LXahedBlCW1_dp_FyvQ3ugUAwUBJDuJCoKf3tgNVs/pub?embedded=true) + +======== For the original version, please go [here](https://github.com/udacity/Sunshine). -A changelog for the course can be found [here](https://docs.google.com/a/knowlabs.com/document/d/193xJb_OpcNCqgquMhxPrMh05IEYFXQqt0S6-6YK8gBw/pub). \ No newline at end of file +A changelog for the course can be found [here](https://docs.google.com/a/knowlabs.com/document/d/193xJb_OpcNCqgquMhxPrMh05IEYFXQqt0S6-6YK8gBw/pub). diff --git a/app/build.gradle b/app/build.gradle index 19167f951..f7ffae8fd 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -17,6 +17,9 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } + buildTypes.each { + it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY', MyOpenWeatherMapApiKey + } } dependencies { diff --git a/app/src/androidTest/java/com/example/android/sunshine/app/ApplicationTest.java b/app/src/androidTest/java/com/example/android/sunshine/app/ApplicationTest.java deleted file mode 100644 index eb830ddb8..000000000 --- a/app/src/androidTest/java/com/example/android/sunshine/app/ApplicationTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.example.android.sunshine.app; - -import android.app.Application; -import android.test.ApplicationTestCase; - -/** - * Testing Fundamentals - */ -public class ApplicationTest extends ApplicationTestCase { - public ApplicationTest() { - super(Application.class); - } -} \ No newline at end of file diff --git a/app/src/androidTest/java/com/example/android/sunshine/app/FullTestSuite.java b/app/src/androidTest/java/com/example/android/sunshine/app/FullTestSuite.java new file mode 100644 index 000000000..0c037a1a2 --- /dev/null +++ b/app/src/androidTest/java/com/example/android/sunshine/app/FullTestSuite.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.sunshine.app; + +import android.test.suitebuilder.TestSuiteBuilder; + +import junit.framework.Test; +import junit.framework.TestSuite; + +public class FullTestSuite extends TestSuite { + public static Test suite() { + return new TestSuiteBuilder(FullTestSuite.class) + .includeAllPackagesUnderHere().build(); + } + + public FullTestSuite() { + super(); + } +} diff --git a/app/src/androidTest/java/com/example/android/sunshine/app/data/TestDb.java b/app/src/androidTest/java/com/example/android/sunshine/app/data/TestDb.java new file mode 100644 index 000000000..c1cd6510d --- /dev/null +++ b/app/src/androidTest/java/com/example/android/sunshine/app/data/TestDb.java @@ -0,0 +1,237 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.sunshine.app.data; + +import android.content.ContentValues; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.test.AndroidTestCase; + +import java.util.HashSet; + +public class TestDb extends AndroidTestCase { + + public static final String LOG_TAG = TestDb.class.getSimpleName(); + + // Since we want each test to start with a clean slate + void deleteTheDatabase() { + mContext.deleteDatabase(WeatherDbHelper.DATABASE_NAME); + } + + /* + This function gets called before each test is executed to delete the database. This makes + sure that we always have a clean test. + */ + public void setUp() { + deleteTheDatabase(); + } + + /* + Students: Uncomment this test once you've written the code to create the Location + table. Note that you will have to have chosen the same column names that I did in + my solution for this test to compile, so if you haven't yet done that, this is + a good time to change your column names to match mine. + + Note that this only tests that the Location table has the correct columns, since we + give you the code for the weather table. This test does not look at the + */ + public void testCreateDb() throws Throwable { + // build a HashSet of all of the table names we wish to look for + // Note that there will be another table in the DB that stores the + // Android metadata (db version information) + final HashSet tableNameHashSet = new HashSet(); + tableNameHashSet.add(WeatherContract.LocationEntry.TABLE_NAME); + tableNameHashSet.add(WeatherContract.WeatherEntry.TABLE_NAME); + + mContext.deleteDatabase(WeatherDbHelper.DATABASE_NAME); + SQLiteDatabase db = new WeatherDbHelper( + this.mContext).getWritableDatabase(); + assertEquals(true, db.isOpen()); + + // have we created the tables we want? + Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null); + + assertTrue("Error: This means that the database has not been created correctly", + c.moveToFirst()); + + // verify that the tables have been created + do { + tableNameHashSet.remove(c.getString(0)); + } while( c.moveToNext() ); + + // if this fails, it means that your database doesn't contain both the location entry + // and weather entry tables + assertTrue("Error: Your database was created without both the location entry and weather entry tables", + tableNameHashSet.isEmpty()); + + // now, do our tables contain the correct columns? + c = db.rawQuery("PRAGMA table_info(" + WeatherContract.LocationEntry.TABLE_NAME + ")", + null); + + assertTrue("Error: This means that we were unable to query the database for table information.", + c.moveToFirst()); + + // Build a HashSet of all of the column names we want to look for + final HashSet locationColumnHashSet = new HashSet(); + locationColumnHashSet.add(WeatherContract.LocationEntry._ID); + locationColumnHashSet.add(WeatherContract.LocationEntry.COLUMN_CITY_NAME); + locationColumnHashSet.add(WeatherContract.LocationEntry.COLUMN_COORD_LAT); + locationColumnHashSet.add(WeatherContract.LocationEntry.COLUMN_COORD_LONG); + locationColumnHashSet.add(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING); + + int columnNameIndex = c.getColumnIndex("name"); + do { + String columnName = c.getString(columnNameIndex); + locationColumnHashSet.remove(columnName); + } while(c.moveToNext()); + + // if this fails, it means that your database doesn't contain all of the required location + // entry columns + assertTrue("Error: The database doesn't contain all of the required location entry columns", + locationColumnHashSet.isEmpty()); + db.close(); + } + + /* + Students: Here is where you will build code to test that we can insert and query the + location database. We've done a lot of work for you. You'll want to look in TestUtilities + where you can uncomment out the "createNorthPoleLocationValues" function. You can + also make use of the ValidateCurrentRecord function from within TestUtilities. + */ + public void testLocationTable() { + insertLocation(); + } + + /* + Students: Here is where you will build code to test that we can insert and query the + database. We've done a lot of work for you. You'll want to look in TestUtilities + where you can use the "createWeatherValues" function. You can + also make use of the validateCurrentRecord function from within TestUtilities. + */ + public void testWeatherTable() { + // First insert the location, and then use the locationRowId to insert + // the weather. Make sure to cover as many failure cases as you can. + + // Instead of rewriting all of the code we've already written in testLocationTable + // we can move this code to insertLocation and then call insertLocation from both + // tests. Why move it? We need the code to return the ID of the inserted location + // and our testLocationTable can only return void because it's a test. + + long locationRowId = insertLocation(); + + // Make sure we have a valid row ID. + assertFalse("Error: Location Not Inserted Correctly", locationRowId == -1L); + + // First step: Get reference to writable database + // If there's an error in those massive SQL table creation Strings, + // errors will be thrown here when you try to get a writable database. + WeatherDbHelper dbHelper = new WeatherDbHelper(mContext); + SQLiteDatabase db = dbHelper.getWritableDatabase(); + + // Second Step (Weather): Create weather values + ContentValues weatherValues = TestUtilities.createWeatherValues(locationRowId); + + // Third Step (Weather): Insert ContentValues into database and get a row ID back + long weatherRowId = db.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, weatherValues); + assertTrue(weatherRowId != -1); + + // Fourth Step: Query the database and receive a Cursor back + // A cursor is your primary interface to the query results. + Cursor weatherCursor = db.query( + WeatherContract.WeatherEntry.TABLE_NAME, // Table to Query + null, // leaving "columns" null just returns all the columns. + null, // cols for "where" clause + null, // values for "where" clause + null, // columns to group by + null, // columns to filter by row groups + null // sort order + ); + + // Move the cursor to the first valid database row and check to see if we have any rows + assertTrue( "Error: No Records returned from location query", weatherCursor.moveToFirst() ); + + // Fifth Step: Validate the location Query + TestUtilities.validateCurrentRecord("testInsertReadDb weatherEntry failed to validate", + weatherCursor, weatherValues); + + // Move the cursor to demonstrate that there is only one record in the database + assertFalse( "Error: More than one record returned from weather query", + weatherCursor.moveToNext() ); + + // Sixth Step: Close cursor and database + weatherCursor.close(); + dbHelper.close(); + } + + + /* + Students: This is a helper method for the testWeatherTable quiz. You can move your + code from testLocationTable to here so that you can call this code from both + testWeatherTable and testLocationTable. + */ + public long insertLocation() { + // First step: Get reference to writable database + // If there's an error in those massive SQL table creation Strings, + // errors will be thrown here when you try to get a writable database. + WeatherDbHelper dbHelper = new WeatherDbHelper(mContext); + SQLiteDatabase db = dbHelper.getWritableDatabase(); + + // Second Step: Create ContentValues of what you want to insert + // (you can use the createNorthPoleLocationValues if you wish) + ContentValues testValues = TestUtilities.createNorthPoleLocationValues(); + + // Third Step: Insert ContentValues into database and get a row ID back + long locationRowId; + locationRowId = db.insert(WeatherContract.LocationEntry.TABLE_NAME, null, testValues); + + // Verify we got a row back. + assertTrue(locationRowId != -1); + + // Data's inserted. IN THEORY. Now pull some out to stare at it and verify it made + // the round trip. + + // Fourth Step: Query the database and receive a Cursor back + // A cursor is your primary interface to the query results. + Cursor cursor = db.query( + WeatherContract.LocationEntry.TABLE_NAME, // Table to Query + null, // all columns + null, // Columns for the "where" clause + null, // Values for the "where" clause + null, // columns to group by + null, // columns to filter by row groups + null // sort order + ); + + // Move the cursor to a valid database row and check to see if we got any records back + // from the query + assertTrue( "Error: No Records returned from location query", cursor.moveToFirst() ); + + // Fifth Step: Validate data in resulting Cursor with the original ContentValues + // (you can use the validateCurrentRecord function in TestUtilities to validate the + // query if you like) + TestUtilities.validateCurrentRecord("Error: Location Query Validation Failed", + cursor, testValues); + + // Move the cursor to demonstrate that there is only one record in the database + assertFalse( "Error: More than one record returned from location query", + cursor.moveToNext() ); + + // Sixth Step: Close Cursor and Database + cursor.close(); + db.close(); + return locationRowId; + } +} diff --git a/app/src/androidTest/java/com/example/android/sunshine/app/data/TestProvider.java b/app/src/androidTest/java/com/example/android/sunshine/app/data/TestProvider.java new file mode 100644 index 000000000..5864d85f0 --- /dev/null +++ b/app/src/androidTest/java/com/example/android/sunshine/app/data/TestProvider.java @@ -0,0 +1,515 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.sunshine.app.data; + +import android.content.ComponentName; +import android.content.ContentUris; +import android.content.ContentValues; +import android.content.pm.PackageManager; +import android.content.pm.ProviderInfo; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.net.Uri; +import android.os.Build; +import android.test.AndroidTestCase; +import android.util.Log; + +import com.example.android.sunshine.app.data.WeatherContract.LocationEntry; +import com.example.android.sunshine.app.data.WeatherContract.WeatherEntry; + +/* + Note: This is not a complete set of tests of the Sunshine ContentProvider, but it does test + that at least the basic functionality has been implemented correctly. + + Students: Uncomment the tests in this class as you implement the functionality in your + ContentProvider to make sure that you've implemented things reasonably correctly. + */ +public class TestProvider extends AndroidTestCase { + + public static final String LOG_TAG = TestProvider.class.getSimpleName(); + + /* + This helper function deletes all records from both database tables using the ContentProvider. + It also queries the ContentProvider to make sure that the database has been successfully + deleted, so it cannot be used until the Query and Delete functions have been written + in the ContentProvider. + + Students: Replace the calls to deleteAllRecordsFromDB with this one after you have written + the delete functionality in the ContentProvider. + */ + public void deleteAllRecordsFromProvider() { + mContext.getContentResolver().delete( + WeatherEntry.CONTENT_URI, + null, + null + ); + mContext.getContentResolver().delete( + LocationEntry.CONTENT_URI, + null, + null + ); + + Cursor cursor = mContext.getContentResolver().query( + WeatherEntry.CONTENT_URI, + null, + null, + null, + null + ); + assertEquals("Error: Records not deleted from Weather table during delete", 0, cursor.getCount()); + cursor.close(); + + cursor = mContext.getContentResolver().query( + LocationEntry.CONTENT_URI, + null, + null, + null, + null + ); + assertEquals("Error: Records not deleted from Location table during delete", 0, cursor.getCount()); + cursor.close(); + } + + /* + Student: Refactor this function to use the deleteAllRecordsFromProvider functionality once + you have implemented delete functionality there. + */ + public void deleteAllRecords() { + deleteAllRecordsFromProvider(); + } + + // Since we want each test to start with a clean slate, run deleteAllRecords + // in setUp (called by the test runner before each test). + @Override + protected void setUp() throws Exception { + super.setUp(); + deleteAllRecords(); + } + + /* + This test checks to make sure that the content provider is registered correctly. + Students: Uncomment this test to make sure you've correctly registered the WeatherProvider. + */ + public void testProviderRegistry() { + PackageManager pm = mContext.getPackageManager(); + + // We define the component name based on the package name from the context and the + // WeatherProvider class. + ComponentName componentName = new ComponentName(mContext.getPackageName(), + WeatherProvider.class.getName()); + try { + // Fetch the provider info using the component name from the PackageManager + // This throws an exception if the provider isn't registered. + ProviderInfo providerInfo = pm.getProviderInfo(componentName, 0); + + // Make sure that the registered authority matches the authority from the Contract. + assertEquals("Error: WeatherProvider registered with authority: " + providerInfo.authority + + " instead of authority: " + WeatherContract.CONTENT_AUTHORITY, + providerInfo.authority, WeatherContract.CONTENT_AUTHORITY); + } catch (PackageManager.NameNotFoundException e) { + // I guess the provider isn't registered correctly. + assertTrue("Error: WeatherProvider not registered at " + mContext.getPackageName(), + false); + } + } + + /* + This test doesn't touch the database. It verifies that the ContentProvider returns + the correct type for each type of URI that it can handle. + Students: Uncomment this test to verify that your implementation of GetType is + functioning correctly. + */ + public void testGetType() { + // content://com.example.android.sunshine.app/weather/ + String type = mContext.getContentResolver().getType(WeatherEntry.CONTENT_URI); + // vnd.android.cursor.dir/com.example.android.sunshine.app/weather + assertEquals("Error: the WeatherEntry CONTENT_URI should return WeatherEntry.CONTENT_TYPE", + WeatherEntry.CONTENT_TYPE, type); + + String testLocation = "94074"; + // content://com.example.android.sunshine.app/weather/94074 + type = mContext.getContentResolver().getType( + WeatherEntry.buildWeatherLocation(testLocation)); + // vnd.android.cursor.dir/com.example.android.sunshine.app/weather + assertEquals("Error: the WeatherEntry CONTENT_URI with location should return WeatherEntry.CONTENT_TYPE", + WeatherEntry.CONTENT_TYPE, type); + + long testDate = 1419120000L; // December 21st, 2014 + // content://com.example.android.sunshine.app/weather/94074/20140612 + type = mContext.getContentResolver().getType( + WeatherEntry.buildWeatherLocationWithDate(testLocation, testDate)); + // vnd.android.cursor.item/com.example.android.sunshine.app/weather/1419120000 + assertEquals("Error: the WeatherEntry CONTENT_URI with location and date should return WeatherEntry.CONTENT_ITEM_TYPE", + WeatherEntry.CONTENT_ITEM_TYPE, type); + + // content://com.example.android.sunshine.app/location/ + type = mContext.getContentResolver().getType(LocationEntry.CONTENT_URI); + // vnd.android.cursor.dir/com.example.android.sunshine.app/location + assertEquals("Error: the LocationEntry CONTENT_URI should return LocationEntry.CONTENT_TYPE", + LocationEntry.CONTENT_TYPE, type); + } + + + /* + This test uses the database directly to insert and then uses the ContentProvider to + read out the data. Uncomment this test to see if the basic weather query functionality + given in the ContentProvider is working correctly. + */ + public void testBasicWeatherQuery() { + // insert our test records into the database + WeatherDbHelper dbHelper = new WeatherDbHelper(mContext); + SQLiteDatabase db = dbHelper.getWritableDatabase(); + + ContentValues testValues = TestUtilities.createNorthPoleLocationValues(); + long locationRowId = TestUtilities.insertNorthPoleLocationValues(mContext); + + // Fantastic. Now that we have a location, add some weather! + ContentValues weatherValues = TestUtilities.createWeatherValues(locationRowId); + + long weatherRowId = db.insert(WeatherEntry.TABLE_NAME, null, weatherValues); + assertTrue("Unable to Insert WeatherEntry into the Database", weatherRowId != -1); + + db.close(); + + // Test the basic content provider query + Cursor weatherCursor = mContext.getContentResolver().query( + WeatherEntry.CONTENT_URI, + null, + null, + null, + null + ); + + // Make sure we get the correct cursor out of the database + TestUtilities.validateCursor("testBasicWeatherQuery", weatherCursor, weatherValues); + } + + /* + This test uses the database directly to insert and then uses the ContentProvider to + read out the data. Uncomment this test to see if your location queries are + performing correctly. + */ + public void testBasicLocationQueries() { + // insert our test records into the database + WeatherDbHelper dbHelper = new WeatherDbHelper(mContext); + SQLiteDatabase db = dbHelper.getWritableDatabase(); + + ContentValues testValues = TestUtilities.createNorthPoleLocationValues(); + long locationRowId = TestUtilities.insertNorthPoleLocationValues(mContext); + + // Test the basic content provider query + Cursor locationCursor = mContext.getContentResolver().query( + LocationEntry.CONTENT_URI, + null, + null, + null, + null + ); + + // Make sure we get the correct cursor out of the database + TestUtilities.validateCursor("testBasicLocationQueries, location query", locationCursor, testValues); + + // Has the NotificationUri been set correctly? --- we can only test this easily against API + // level 19 or greater because getNotificationUri was added in API level 19. + if ( Build.VERSION.SDK_INT >= 19 ) { + assertEquals("Error: Location Query did not properly set NotificationUri", + locationCursor.getNotificationUri(), LocationEntry.CONTENT_URI); + } + } + + /* + This test uses the provider to insert and then update the data. Uncomment this test to + see if your update location is functioning correctly. + */ + public void testUpdateLocation() { + // Create a new map of values, where column names are the keys + ContentValues values = TestUtilities.createNorthPoleLocationValues(); + + Uri locationUri = mContext.getContentResolver(). + insert(LocationEntry.CONTENT_URI, values); + long locationRowId = ContentUris.parseId(locationUri); + + // Verify we got a row back. + assertTrue(locationRowId != -1); + Log.d(LOG_TAG, "New row id: " + locationRowId); + + ContentValues updatedValues = new ContentValues(values); + updatedValues.put(LocationEntry._ID, locationRowId); + updatedValues.put(LocationEntry.COLUMN_CITY_NAME, "Santa's Village"); + + // Create a cursor with observer to make sure that the content provider is notifying + // the observers as expected + Cursor locationCursor = mContext.getContentResolver().query(LocationEntry.CONTENT_URI, null, null, null, null); + + TestUtilities.TestContentObserver tco = TestUtilities.getTestContentObserver(); + locationCursor.registerContentObserver(tco); + + int count = mContext.getContentResolver().update( + LocationEntry.CONTENT_URI, updatedValues, LocationEntry._ID + "= ?", + new String[] { Long.toString(locationRowId)}); + assertEquals(count, 1); + + // Test to make sure our observer is called. If not, we throw an assertion. + // + // Students: If your code is failing here, it means that your content provider + // isn't calling getContext().getContentResolver().notifyChange(uri, null); + tco.waitForNotificationOrFail(); + + locationCursor.unregisterContentObserver(tco); + locationCursor.close(); + + // A cursor is your primary interface to the query results. + Cursor cursor = mContext.getContentResolver().query( + LocationEntry.CONTENT_URI, + null, // projection + LocationEntry._ID + " = " + locationRowId, + null, // Values for the "where" clause + null // sort order + ); + + TestUtilities.validateCursor("testUpdateLocation. Error validating location entry update.", + cursor, updatedValues); + + cursor.close(); + } + + + // Make sure we can still delete after adding/updating stuff + // + // Student: Uncomment this test after you have completed writing the insert functionality + // in your provider. It relies on insertions with testInsertReadProvider, so insert and + // query functionality must also be complete before this test can be used. + public void testInsertReadProvider() { + ContentValues testValues = TestUtilities.createNorthPoleLocationValues(); + + // Register a content observer for our insert. This time, directly with the content resolver + TestUtilities.TestContentObserver tco = TestUtilities.getTestContentObserver(); + mContext.getContentResolver().registerContentObserver(LocationEntry.CONTENT_URI, true, tco); + Uri locationUri = mContext.getContentResolver().insert(LocationEntry.CONTENT_URI, testValues); + + // Did our content observer get called? Students: If this fails, your insert location + // isn't calling getContext().getContentResolver().notifyChange(uri, null); + tco.waitForNotificationOrFail(); + mContext.getContentResolver().unregisterContentObserver(tco); + + long locationRowId = ContentUris.parseId(locationUri); + + // Verify we got a row back. + assertTrue(locationRowId != -1); + + // Data's inserted. IN THEORY. Now pull some out to stare at it and verify it made + // the round trip. + + // A cursor is your primary interface to the query results. + Cursor cursor = mContext.getContentResolver().query( + LocationEntry.CONTENT_URI, + null, // leaving "columns" null just returns all the columns. + null, // cols for "where" clause + null, // values for "where" clause + null // sort order + ); + + TestUtilities.validateCursor("testInsertReadProvider. Error validating LocationEntry.", + cursor, testValues); + + // Fantastic. Now that we have a location, add some weather! + ContentValues weatherValues = TestUtilities.createWeatherValues(locationRowId); + // The TestContentObserver is a one-shot class + tco = TestUtilities.getTestContentObserver(); + + mContext.getContentResolver().registerContentObserver(WeatherEntry.CONTENT_URI, true, tco); + + Uri weatherInsertUri = mContext.getContentResolver() + .insert(WeatherEntry.CONTENT_URI, weatherValues); + assertTrue(weatherInsertUri != null); + + // Did our content observer get called? Students: If this fails, your insert weather + // in your ContentProvider isn't calling + // getContext().getContentResolver().notifyChange(uri, null); + tco.waitForNotificationOrFail(); + mContext.getContentResolver().unregisterContentObserver(tco); + + // A cursor is your primary interface to the query results. + Cursor weatherCursor = mContext.getContentResolver().query( + WeatherEntry.CONTENT_URI, // Table to Query + null, // leaving "columns" null just returns all the columns. + null, // cols for "where" clause + null, // values for "where" clause + null // columns to group by + ); + + TestUtilities.validateCursor("testInsertReadProvider. Error validating WeatherEntry insert.", + weatherCursor, weatherValues); + + // Add the location values in with the weather data so that we can make + // sure that the join worked and we actually get all the values back + weatherValues.putAll(testValues); + + // Get the joined Weather and Location data + weatherCursor = mContext.getContentResolver().query( + WeatherEntry.buildWeatherLocation(TestUtilities.TEST_LOCATION), + null, // leaving "columns" null just returns all the columns. + null, // cols for "where" clause + null, // values for "where" clause + null // sort order + ); + TestUtilities.validateCursor("testInsertReadProvider. Error validating joined Weather and Location Data.", + weatherCursor, weatherValues); + + // Get the joined Weather and Location data with a start date + weatherCursor = mContext.getContentResolver().query( + WeatherEntry.buildWeatherLocationWithStartDate( + TestUtilities.TEST_LOCATION, TestUtilities.TEST_DATE), + null, // leaving "columns" null just returns all the columns. + null, // cols for "where" clause + null, // values for "where" clause + null // sort order + ); + TestUtilities.validateCursor("testInsertReadProvider. Error validating joined Weather and Location Data with start date.", + weatherCursor, weatherValues); + + // Get the joined Weather data for a specific date + weatherCursor = mContext.getContentResolver().query( + WeatherEntry.buildWeatherLocationWithDate(TestUtilities.TEST_LOCATION, TestUtilities.TEST_DATE), + null, + null, + null, + null + ); + TestUtilities.validateCursor("testInsertReadProvider. Error validating joined Weather and Location data for a specific date.", + weatherCursor, weatherValues); + } + + // Make sure we can still delete after adding/updating stuff + // + // Student: Uncomment this test after you have completed writing the delete functionality + // in your provider. It relies on insertions with testInsertReadProvider, so insert and + // query functionality must also be complete before this test can be used. + public void testDeleteRecords() { + testInsertReadProvider(); + + // Register a content observer for our location delete. + TestUtilities.TestContentObserver locationObserver = TestUtilities.getTestContentObserver(); + mContext.getContentResolver().registerContentObserver(LocationEntry.CONTENT_URI, true, locationObserver); + + // Register a content observer for our weather delete. + TestUtilities.TestContentObserver weatherObserver = TestUtilities.getTestContentObserver(); + mContext.getContentResolver().registerContentObserver(WeatherEntry.CONTENT_URI, true, weatherObserver); + + deleteAllRecordsFromProvider(); + + // Students: If either of these fail, you most-likely are not calling the + // getContext().getContentResolver().notifyChange(uri, null); in the ContentProvider + // delete. (only if the insertReadProvider is succeeding) + locationObserver.waitForNotificationOrFail(); + weatherObserver.waitForNotificationOrFail(); + + mContext.getContentResolver().unregisterContentObserver(locationObserver); + mContext.getContentResolver().unregisterContentObserver(weatherObserver); + } + + + static private final int BULK_INSERT_RECORDS_TO_INSERT = 10; + static ContentValues[] createBulkInsertWeatherValues(long locationRowId) { + long currentTestDate = TestUtilities.TEST_DATE; + long millisecondsInADay = 1000*60*60*24; + ContentValues[] returnContentValues = new ContentValues[BULK_INSERT_RECORDS_TO_INSERT]; + + for ( int i = 0; i < BULK_INSERT_RECORDS_TO_INSERT; i++, currentTestDate+= millisecondsInADay ) { + ContentValues weatherValues = new ContentValues(); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_LOC_KEY, locationRowId); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_DATE, currentTestDate); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_DEGREES, 1.1); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_HUMIDITY, 1.2 + 0.01 * (float) i); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_PRESSURE, 1.3 - 0.01 * (float) i); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_MAX_TEMP, 75 + i); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_MIN_TEMP, 65 - i); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_SHORT_DESC, "Asteroids"); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_WIND_SPEED, 5.5 + 0.2 * (float) i); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_WEATHER_ID, 321); + returnContentValues[i] = weatherValues; + } + return returnContentValues; + } + + // Student: Uncomment this test after you have completed writing the BulkInsert functionality + // in your provider. Note that this test will work with the built-in (default) provider + // implementation, which just inserts records one-at-a-time, so really do implement the + // BulkInsert ContentProvider function. + public void testBulkInsert() { + // first, let's create a location value + ContentValues testValues = TestUtilities.createNorthPoleLocationValues(); + Uri locationUri = mContext.getContentResolver().insert(LocationEntry.CONTENT_URI, testValues); + long locationRowId = ContentUris.parseId(locationUri); + + // Verify we got a row back. + assertTrue(locationRowId != -1); + + // Data's inserted. IN THEORY. Now pull some out to stare at it and verify it made + // the round trip. + + // A cursor is your primary interface to the query results. + Cursor cursor = mContext.getContentResolver().query( + LocationEntry.CONTENT_URI, + null, // leaving "columns" null just returns all the columns. + null, // cols for "where" clause + null, // values for "where" clause + null // sort order + ); + + TestUtilities.validateCursor("testBulkInsert. Error validating LocationEntry.", + cursor, testValues); + + // Now we can bulkInsert some weather. In fact, we only implement BulkInsert for weather + // entries. With ContentProviders, you really only have to implement the features you + // use, after all. + ContentValues[] bulkInsertContentValues = createBulkInsertWeatherValues(locationRowId); + + // Register a content observer for our bulk insert. + TestUtilities.TestContentObserver weatherObserver = TestUtilities.getTestContentObserver(); + mContext.getContentResolver().registerContentObserver(WeatherEntry.CONTENT_URI, true, weatherObserver); + + int insertCount = mContext.getContentResolver().bulkInsert(WeatherEntry.CONTENT_URI, bulkInsertContentValues); + + // Students: If this fails, it means that you most-likely are not calling the + // getContext().getContentResolver().notifyChange(uri, null); in your BulkInsert + // ContentProvider method. + weatherObserver.waitForNotificationOrFail(); + mContext.getContentResolver().unregisterContentObserver(weatherObserver); + + assertEquals(insertCount, BULK_INSERT_RECORDS_TO_INSERT); + + // A cursor is your primary interface to the query results. + cursor = mContext.getContentResolver().query( + WeatherEntry.CONTENT_URI, + null, // leaving "columns" null just returns all the columns. + null, // cols for "where" clause + null, // values for "where" clause + WeatherEntry.COLUMN_DATE + " ASC" // sort order == by DATE ASCENDING + ); + + // we should have as many records in the database as we've inserted + assertEquals(cursor.getCount(), BULK_INSERT_RECORDS_TO_INSERT); + + // and let's make sure they match the ones we created + cursor.moveToFirst(); + for ( int i = 0; i < BULK_INSERT_RECORDS_TO_INSERT; i++, cursor.moveToNext() ) { + TestUtilities.validateCurrentRecord("testBulkInsert. Error validating WeatherEntry " + i, + cursor, bulkInsertContentValues[i]); + } + cursor.close(); + } +} diff --git a/app/src/androidTest/java/com/example/android/sunshine/app/data/TestUriMatcher.java b/app/src/androidTest/java/com/example/android/sunshine/app/data/TestUriMatcher.java new file mode 100644 index 000000000..ab692eb58 --- /dev/null +++ b/app/src/androidTest/java/com/example/android/sunshine/app/data/TestUriMatcher.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.sunshine.app.data; + +import android.content.UriMatcher; +import android.net.Uri; +import android.test.AndroidTestCase; + +/* + Uncomment this class when you are ready to test your UriMatcher. Note that this class utilizes + constants that are declared with package protection inside of the UriMatcher, which is why + the test must be in the same data package as the Android app code. Doing the test this way is + a nice compromise between data hiding and testability. + */ +public class TestUriMatcher extends AndroidTestCase { + private static final String LOCATION_QUERY = "London, UK"; + private static final long TEST_DATE = 1419033600L; // December 20th, 2014 + private static final long TEST_LOCATION_ID = 10L; + + // content://com.example.android.sunshine.app/weather" + private static final Uri TEST_WEATHER_DIR = WeatherContract.WeatherEntry.CONTENT_URI; + private static final Uri TEST_WEATHER_WITH_LOCATION_DIR = WeatherContract.WeatherEntry.buildWeatherLocation(LOCATION_QUERY); + private static final Uri TEST_WEATHER_WITH_LOCATION_AND_DATE_DIR = WeatherContract.WeatherEntry.buildWeatherLocationWithDate(LOCATION_QUERY, TEST_DATE); + // content://com.example.android.sunshine.app/location" + private static final Uri TEST_LOCATION_DIR = WeatherContract.LocationEntry.CONTENT_URI; + + /* + Students: This function tests that your UriMatcher returns the correct integer value + for each of the Uri types that our ContentProvider can handle. Uncomment this when you are + ready to test your UriMatcher. + */ + public void testUriMatcher() { + UriMatcher testMatcher = WeatherProvider.buildUriMatcher(); + + assertEquals("Error: The WEATHER URI was matched incorrectly.", + testMatcher.match(TEST_WEATHER_DIR), WeatherProvider.WEATHER); + assertEquals("Error: The WEATHER WITH LOCATION URI was matched incorrectly.", + testMatcher.match(TEST_WEATHER_WITH_LOCATION_DIR), WeatherProvider.WEATHER_WITH_LOCATION); + assertEquals("Error: The WEATHER WITH LOCATION AND DATE URI was matched incorrectly.", + testMatcher.match(TEST_WEATHER_WITH_LOCATION_AND_DATE_DIR), WeatherProvider.WEATHER_WITH_LOCATION_AND_DATE); + assertEquals("Error: The LOCATION URI was matched incorrectly.", + testMatcher.match(TEST_LOCATION_DIR), WeatherProvider.LOCATION); + } +} diff --git a/app/src/androidTest/java/com/example/android/sunshine/app/data/TestUtilities.java b/app/src/androidTest/java/com/example/android/sunshine/app/data/TestUtilities.java new file mode 100644 index 000000000..593957231 --- /dev/null +++ b/app/src/androidTest/java/com/example/android/sunshine/app/data/TestUtilities.java @@ -0,0 +1,151 @@ +package com.example.android.sunshine.app.data; + +import android.content.ContentValues; +import android.content.Context; +import android.database.ContentObserver; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.net.Uri; +import android.os.Handler; +import android.os.HandlerThread; +import android.test.AndroidTestCase; + +import com.example.android.sunshine.app.utils.PollingCheck; + +import java.util.Map; +import java.util.Set; + +/* + Students: These are functions and some test data to make it easier to test your database and + Content Provider. Note that you'll want your WeatherContract class to exactly match the one + in our solution to use these as-given. + */ +public class TestUtilities extends AndroidTestCase { + static final String TEST_LOCATION = "99705"; + static final long TEST_DATE = 1419033600L; // December 20th, 2014 + + static void validateCursor(String error, Cursor valueCursor, ContentValues expectedValues) { + assertTrue("Empty cursor returned. " + error, valueCursor.moveToFirst()); + validateCurrentRecord(error, valueCursor, expectedValues); + valueCursor.close(); + } + + static void validateCurrentRecord(String error, Cursor valueCursor, ContentValues expectedValues) { + Set> valueSet = expectedValues.valueSet(); + for (Map.Entry entry : valueSet) { + String columnName = entry.getKey(); + int idx = valueCursor.getColumnIndex(columnName); + assertFalse("Column '" + columnName + "' not found. " + error, idx == -1); + String expectedValue = entry.getValue().toString(); + assertEquals("Value '" + entry.getValue().toString() + + "' did not match the expected value '" + + expectedValue + "'. " + error, expectedValue, valueCursor.getString(idx)); + } + } + + /* + Students: Use this to create some default weather values for your database tests. + */ + static ContentValues createWeatherValues(long locationRowId) { + ContentValues weatherValues = new ContentValues(); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_LOC_KEY, locationRowId); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_DATE, TEST_DATE); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_DEGREES, 1.1); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_HUMIDITY, 1.2); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_PRESSURE, 1.3); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_MAX_TEMP, 75); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_MIN_TEMP, 65); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_SHORT_DESC, "Asteroids"); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_WIND_SPEED, 5.5); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_WEATHER_ID, 321); + + return weatherValues; + } + + /* + Students: You can uncomment this helper function once you have finished creating the + LocationEntry part of the WeatherContract. + */ + static ContentValues createNorthPoleLocationValues() { + // Create a new map of values, where column names are the keys + ContentValues testValues = new ContentValues(); + testValues.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, TEST_LOCATION); + testValues.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, "North Pole"); + testValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, 64.7488); + testValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, -147.353); + + return testValues; + } + + /* + Students: You can uncomment this function once you have finished creating the + LocationEntry part of the WeatherContract as well as the WeatherDbHelper. + */ + static long insertNorthPoleLocationValues(Context context) { + // insert our test records into the database + WeatherDbHelper dbHelper = new WeatherDbHelper(context); + SQLiteDatabase db = dbHelper.getWritableDatabase(); + ContentValues testValues = TestUtilities.createNorthPoleLocationValues(); + + long locationRowId; + locationRowId = db.insert(WeatherContract.LocationEntry.TABLE_NAME, null, testValues); + + // Verify we got a row back. + assertTrue("Error: Failure to insert North Pole Location Values", locationRowId != -1); + + return locationRowId; + } + + /* + Students: The functions we provide inside of TestProvider use this utility class to test + the ContentObserver callbacks using the PollingCheck class that we grabbed from the Android + CTS tests. + + Note that this only tests that the onChange function is called; it does not test that the + correct Uri is returned. + */ + static class TestContentObserver extends ContentObserver { + final HandlerThread mHT; + boolean mContentChanged; + + static TestContentObserver getTestContentObserver() { + HandlerThread ht = new HandlerThread("ContentObserverThread"); + ht.start(); + return new TestContentObserver(ht); + } + + private TestContentObserver(HandlerThread ht) { + super(new Handler(ht.getLooper())); + mHT = ht; + } + + // On earlier versions of Android, this onChange method is called + @Override + public void onChange(boolean selfChange) { + onChange(selfChange, null); + } + + @Override + public void onChange(boolean selfChange, Uri uri) { + mContentChanged = true; + } + + public void waitForNotificationOrFail() { + // Note: The PollingCheck class is taken from the Android CTS (Compatibility Test Suite). + // It's useful to look at the Android CTS source for ideas on how to test your Android + // applications. The reason that PollingCheck works is that, by default, the JUnit + // testing framework is not running on the main Android application thread. + new PollingCheck(5000) { + @Override + protected boolean check() { + return mContentChanged; + } + }.run(); + mHT.quit(); + } + } + + static TestContentObserver getTestContentObserver() { + return TestContentObserver.getTestContentObserver(); + } +} diff --git a/app/src/androidTest/java/com/example/android/sunshine/app/data/TestWeatherContract.java b/app/src/androidTest/java/com/example/android/sunshine/app/data/TestWeatherContract.java new file mode 100644 index 000000000..6a2099814 --- /dev/null +++ b/app/src/androidTest/java/com/example/android/sunshine/app/data/TestWeatherContract.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.sunshine.app.data; + +import android.net.Uri; +import android.test.AndroidTestCase; + +/* + Students: This is NOT a complete test for the WeatherContract --- just for the functions + that we expect you to write. + */ +public class TestWeatherContract extends AndroidTestCase { + + // intentionally includes a slash to make sure Uri is getting quoted correctly + private static final String TEST_WEATHER_LOCATION = "/North Pole"; + private static final long TEST_WEATHER_DATE = 1419033600L; // December 20th, 2014 + + /* + Students: Uncomment this out to test your weather location function. + */ + public void testBuildWeatherLocation() { + Uri locationUri = WeatherContract.WeatherEntry.buildWeatherLocation(TEST_WEATHER_LOCATION); + assertNotNull("Error: Null Uri returned. You must fill-in buildWeatherLocation in " + + "WeatherContract.", + locationUri); + assertEquals("Error: Weather location not properly appended to the end of the Uri", + TEST_WEATHER_LOCATION, locationUri.getLastPathSegment()); + assertEquals("Error: Weather location Uri doesn't match our expected result", + locationUri.toString(), + "content://com.example.android.sunshine.app/weather/%2FNorth%20Pole"); + } +} diff --git a/app/src/androidTest/java/com/example/android/sunshine/app/utils/PollingCheck.java b/app/src/androidTest/java/com/example/android/sunshine/app/utils/PollingCheck.java new file mode 100644 index 000000000..733d503d0 --- /dev/null +++ b/app/src/androidTest/java/com/example/android/sunshine/app/utils/PollingCheck.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Note: This file copied from the Android CTS Tests + */ +package com.example.android.sunshine.app.utils; + +import junit.framework.Assert; + +import java.util.concurrent.Callable; + +public abstract class PollingCheck { + private static final long TIME_SLICE = 50; + private long mTimeout = 3000; + + public PollingCheck() { + } + + public PollingCheck(long timeout) { + mTimeout = timeout; + } + + protected abstract boolean check(); + + public void run() { + if (check()) { + return; + } + + long timeout = mTimeout; + while (timeout > 0) { + try { + Thread.sleep(TIME_SLICE); + } catch (InterruptedException e) { + Assert.fail("unexpected InterruptedException"); + } + + if (check()) { + return; + } + + timeout -= TIME_SLICE; + } + + Assert.fail("unexpected timeout"); + } + + public static void check(CharSequence message, long timeout, Callable condition) + throws Exception { + while (timeout > 0) { + if (condition.call()) { + return; + } + + Thread.sleep(TIME_SLICE); + timeout -= TIME_SLICE; + } + + Assert.fail(message.toString()); + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 64a14ce2b..7b09e4b73 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -5,13 +5,22 @@ + + + + + @@ -27,6 +36,43 @@ android:name="android.support.PARENT_ACTIVITY" android:value="com.example.android.sunshine.app.MainActivity" /> + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/java/com/example/android/sunshine/app/DetailActivity.java b/app/src/main/java/com/example/android/sunshine/app/DetailActivity.java index c934a5eaf..2f3271515 100644 --- a/app/src/main/java/com/example/android/sunshine/app/DetailActivity.java +++ b/app/src/main/java/com/example/android/sunshine/app/DetailActivity.java @@ -13,19 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package com.example.android.sunshine.app; -import android.support.v7.app.ActionBarActivity; import android.content.Intent; import android.os.Bundle; -import android.support.v4.app.Fragment; -import android.view.LayoutInflater; +import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; -import android.widget.TextView; + public class DetailActivity extends ActionBarActivity { @@ -33,14 +28,23 @@ public class DetailActivity extends ActionBarActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); + if (savedInstanceState == null) { + // Create the detail fragment and add it to the activity + // using a fragment transaction. + + Bundle arguments = new Bundle(); + arguments.putParcelable(DetailFragment.DETAIL_URI, getIntent().getData()); + + DetailFragment fragment = new DetailFragment(); + fragment.setArguments(arguments); + getSupportFragmentManager().beginTransaction() - .add(R.id.container, new DetailFragment()) + .add(R.id.weather_detail_container, fragment) .commit(); } } - @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. @@ -54,38 +58,10 @@ public boolean onOptionsItemSelected(MenuItem item) { // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); - - //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { + startActivity(new Intent(this, SettingsActivity.class)); return true; } - return super.onOptionsItemSelected(item); } - - /** - * A placeholder fragment containing a simple view. - */ - public static class DetailFragment extends Fragment { - - public DetailFragment() { - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - - View rootView = inflater.inflate(R.layout.fragment_detail, container, false); - - // The detail Activity called via intent. Inspect the intent for forecast data. - Intent intent = getActivity().getIntent(); - if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) { - String forecastStr = intent.getStringExtra(Intent.EXTRA_TEXT); - ((TextView) rootView.findViewById(R.id.detail_text)) - .setText(forecastStr); - } - - return rootView; - } - } -} +} \ No newline at end of file diff --git a/app/src/main/java/com/example/android/sunshine/app/DetailFragment.java b/app/src/main/java/com/example/android/sunshine/app/DetailFragment.java new file mode 100644 index 000000000..ed783b699 --- /dev/null +++ b/app/src/main/java/com/example/android/sunshine/app/DetailFragment.java @@ -0,0 +1,240 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.sunshine.app; + +import android.content.Intent; +import android.database.Cursor; +import android.net.Uri; +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v4.app.LoaderManager; +import android.support.v4.content.CursorLoader; +import android.support.v4.content.Loader; +import android.support.v4.view.MenuItemCompat; +import android.support.v7.widget.ShareActionProvider; +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.ImageView; +import android.widget.TextView; + +import com.example.android.sunshine.app.data.WeatherContract; +import com.example.android.sunshine.app.data.WeatherContract.WeatherEntry; + +/** + * A placeholder fragment containing a simple view. + */ +public class DetailFragment extends Fragment implements LoaderManager.LoaderCallbacks { + + private static final String LOG_TAG = DetailFragment.class.getSimpleName(); + static final String DETAIL_URI = "URI"; + + private static final String FORECAST_SHARE_HASHTAG = " #SunshineApp"; + + private ShareActionProvider mShareActionProvider; + private String mForecast; + private Uri mUri; + + private static final int DETAIL_LOADER = 0; + + private static final String[] DETAIL_COLUMNS = { + WeatherEntry.TABLE_NAME + "." + WeatherEntry._ID, + WeatherEntry.COLUMN_DATE, + WeatherEntry.COLUMN_SHORT_DESC, + WeatherEntry.COLUMN_MAX_TEMP, + WeatherEntry.COLUMN_MIN_TEMP, + WeatherEntry.COLUMN_HUMIDITY, + WeatherEntry.COLUMN_PRESSURE, + WeatherEntry.COLUMN_WIND_SPEED, + WeatherEntry.COLUMN_DEGREES, + WeatherEntry.COLUMN_WEATHER_ID, + // This works because the WeatherProvider returns location data joined with + // weather data, even though they're stored in two different tables. + WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + }; + + // These indices are tied to DETAIL_COLUMNS. If DETAIL_COLUMNS changes, these + // must change. + public static final int COL_WEATHER_ID = 0; + public static final int COL_WEATHER_DATE = 1; + public static final int COL_WEATHER_DESC = 2; + public static final int COL_WEATHER_MAX_TEMP = 3; + public static final int COL_WEATHER_MIN_TEMP = 4; + public static final int COL_WEATHER_HUMIDITY = 5; + public static final int COL_WEATHER_PRESSURE = 6; + public static final int COL_WEATHER_WIND_SPEED = 7; + public static final int COL_WEATHER_DEGREES = 8; + public static final int COL_WEATHER_CONDITION_ID = 9; + + private ImageView mIconView; + private TextView mFriendlyDateView; + private TextView mDateView; + private TextView mDescriptionView; + private TextView mHighTempView; + private TextView mLowTempView; + private TextView mHumidityView; + private TextView mWindView; + private TextView mPressureView; + + public DetailFragment() { + setHasOptionsMenu(true); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + + Bundle arguments = getArguments(); + if (arguments != null) { + mUri = arguments.getParcelable(DetailFragment.DETAIL_URI); + } + + View 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); + mDescriptionView = (TextView) rootView.findViewById(R.id.detail_forecast_textview); + mHighTempView = (TextView) rootView.findViewById(R.id.detail_high_textview); + mLowTempView = (TextView) rootView.findViewById(R.id.detail_low_textview); + mHumidityView = (TextView) rootView.findViewById(R.id.detail_humidity_textview); + mWindView = (TextView) rootView.findViewById(R.id.detail_wind_textview); + mPressureView = (TextView) rootView.findViewById(R.id.detail_pressure_textview); + return rootView; + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + // Inflate the menu; this adds items to the action bar if it is present. + inflater.inflate(R.menu.detailfragment, menu); + + // Retrieve the share menu item + MenuItem menuItem = menu.findItem(R.id.action_share); + + // Get the provider and hold onto it to set/change the share intent. + mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem); + + // If onLoadFinished happens before this, we can go ahead and set the share intent now. + if (mForecast != null) { + mShareActionProvider.setShareIntent(createShareForecastIntent()); + } + } + + private Intent createShareForecastIntent() { + Intent shareIntent = new Intent(Intent.ACTION_SEND); + shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); + shareIntent.setType("text/plain"); + shareIntent.putExtra(Intent.EXTRA_TEXT, mForecast + FORECAST_SHARE_HASHTAG); + return shareIntent; + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + getLoaderManager().initLoader(DETAIL_LOADER, null, this); + super.onActivityCreated(savedInstanceState); + } + + void onLocationChanged( String newLocation ) { + // replace the uri, since the location has changed + Uri uri = mUri; + if (null != uri) { + long date = WeatherContract.WeatherEntry.getDateFromUri(uri); + Uri updatedUri = WeatherContract.WeatherEntry.buildWeatherLocationWithDate(newLocation, date); + mUri = updatedUri; + getLoaderManager().restartLoader(DETAIL_LOADER, null, this); + } + } + + @Override + public Loader onCreateLoader(int id, Bundle args) { + if ( null != mUri ) { + // Now create and return a CursorLoader that will take care of + // creating a Cursor for the data being displayed. + return new CursorLoader( + getActivity(), + mUri, + DETAIL_COLUMNS, + null, + null, + null + ); + } + return null; + } + + @Override + public void onLoadFinished(Loader loader, Cursor data) { + if (data != null && data.moveToFirst()) { + // Read weather condition ID from cursor + int weatherId = data.getInt(COL_WEATHER_CONDITION_ID); + + // Use weather art image + mIconView.setImageResource(Utility.getArtResourceForWeatherCondition(weatherId)); + + // Read date from cursor and update views for day of week and date + long date = data.getLong(COL_WEATHER_DATE); + String friendlyDateText = Utility.getDayName(getActivity(), date); + String dateText = Utility.getFormattedMonthDay(getActivity(), date); + mFriendlyDateView.setText(friendlyDateText); + mDateView.setText(dateText); + + // Read description from cursor and update view + String description = data.getString(COL_WEATHER_DESC); + mDescriptionView.setText(description); + + // For accessibility, add a content description to the icon field + mIconView.setContentDescription(description); + + // Read high temperature from cursor and update view + boolean isMetric = Utility.isMetric(getActivity()); + + double high = data.getDouble(COL_WEATHER_MAX_TEMP); + String highString = Utility.formatTemperature(getActivity(), high); + mHighTempView.setText(highString); + + // Read low temperature from cursor and update view + double low = data.getDouble(COL_WEATHER_MIN_TEMP); + String lowString = Utility.formatTemperature(getActivity(), low); + mLowTempView.setText(lowString); + + // Read humidity from cursor and update view + float humidity = data.getFloat(COL_WEATHER_HUMIDITY); + mHumidityView.setText(getActivity().getString(R.string.format_humidity, humidity)); + + // Read wind speed and direction from cursor and update view + float windSpeedStr = data.getFloat(COL_WEATHER_WIND_SPEED); + float windDirStr = data.getFloat(COL_WEATHER_DEGREES); + mWindView.setText(Utility.getFormattedWind(getActivity(), windSpeedStr, windDirStr)); + + // Read pressure from cursor and update view + float pressure = data.getFloat(COL_WEATHER_PRESSURE); + mPressureView.setText(getActivity().getString(R.string.format_pressure, pressure)); + + // We still need this for the share intent + mForecast = String.format("%s - %s - %s/%s", dateText, description, high, low); + + // If onCreateOptionsMenu has already happened, we need to update the share intent now. + if (mShareActionProvider != null) { + mShareActionProvider.setShareIntent(createShareForecastIntent()); + } + } + } + + @Override + public void onLoaderReset(Loader loader) { } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/android/sunshine/app/ForecastAdapter.java b/app/src/main/java/com/example/android/sunshine/app/ForecastAdapter.java new file mode 100644 index 000000000..235cb6183 --- /dev/null +++ b/app/src/main/java/com/example/android/sunshine/app/ForecastAdapter.java @@ -0,0 +1,131 @@ +package com.example.android.sunshine.app; + +import android.content.Context; +import android.database.Cursor; +import android.support.v4.widget.CursorAdapter; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +/** + * {@link ForecastAdapter} exposes a list of weather forecasts + * from a {@link Cursor} to a {@link android.widget.ListView}. + */ +public class ForecastAdapter extends CursorAdapter { + + private static final int VIEW_TYPE_COUNT = 2; + private static final int VIEW_TYPE_TODAY = 0; + private static final int VIEW_TYPE_FUTURE_DAY = 1; + + // Flag to determine if we want to use a separate view for "today". + private boolean mUseTodayLayout = true; + + /** + * Cache of the children views for a forecast list item. + */ + public static class ViewHolder { + public final ImageView iconView; + public final TextView dateView; + public final TextView descriptionView; + public final TextView highTempView; + public final TextView lowTempView; + + public ViewHolder(View view) { + iconView = (ImageView) view.findViewById(R.id.list_item_icon); + dateView = (TextView) view.findViewById(R.id.list_item_date_textview); + descriptionView = (TextView) view.findViewById(R.id.list_item_forecast_textview); + highTempView = (TextView) view.findViewById(R.id.list_item_high_textview); + lowTempView = (TextView) view.findViewById(R.id.list_item_low_textview); + } + } + + public ForecastAdapter(Context context, Cursor c, int flags) { + super(context, c, flags); + } + + @Override + public View newView(Context context, Cursor cursor, ViewGroup parent) { + // Choose the layout type + int viewType = getItemViewType(cursor.getPosition()); + int layoutId = -1; + switch (viewType) { + case VIEW_TYPE_TODAY: { + layoutId = R.layout.list_item_forecast_today; + break; + } + case VIEW_TYPE_FUTURE_DAY: { + layoutId = R.layout.list_item_forecast; + break; + } + } + + View view = LayoutInflater.from(context).inflate(layoutId, parent, false); + + ViewHolder viewHolder = new ViewHolder(view); + view.setTag(viewHolder); + + return view; + } + + @Override + public void bindView(View view, Context context, Cursor cursor) { + + ViewHolder viewHolder = (ViewHolder) view.getTag(); + + int viewType = getItemViewType(cursor.getPosition()); + switch (viewType) { + case VIEW_TYPE_TODAY: { + // Get weather icon + viewHolder.iconView.setImageResource(Utility.getArtResourceForWeatherCondition( + cursor.getInt(ForecastFragment.COL_WEATHER_CONDITION_ID))); + break; + } + case VIEW_TYPE_FUTURE_DAY: { + // Get weather icon + viewHolder.iconView.setImageResource(Utility.getIconResourceForWeatherCondition( + cursor.getInt(ForecastFragment.COL_WEATHER_CONDITION_ID))); + break; + } + } + + // Read date from cursor + long dateInMillis = cursor.getLong(ForecastFragment.COL_WEATHER_DATE); + // Find TextView and set formatted date on it + viewHolder.dateView.setText(Utility.getFriendlyDayString(context, dateInMillis)); + + // Read weather forecast from cursor + String description = cursor.getString(ForecastFragment.COL_WEATHER_DESC); + // Find TextView and set weather forecast on it + viewHolder.descriptionView.setText(description); + + // For accessibility, add a content description to the icon field + viewHolder.iconView.setContentDescription(description); + + // Read user preference for metric or imperial temperature units + boolean isMetric = Utility.isMetric(context); + + // Read high temperature from cursor + double high = cursor.getDouble(ForecastFragment.COL_WEATHER_MAX_TEMP); + viewHolder.highTempView.setText(Utility.formatTemperature(context, high)); + + // Read low temperature from cursor + double low = cursor.getDouble(ForecastFragment.COL_WEATHER_MIN_TEMP); + viewHolder.lowTempView.setText(Utility.formatTemperature(context, low)); + } + + public void setUseTodayLayout(boolean useTodayLayout) { + mUseTodayLayout = useTodayLayout; + } + + @Override + public int getItemViewType(int position) { + return (position == 0 && mUseTodayLayout) ? VIEW_TYPE_TODAY : VIEW_TYPE_FUTURE_DAY; + } + + @Override + public int getViewTypeCount() { + return VIEW_TYPE_COUNT; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/android/sunshine/app/ForecastFragment.java b/app/src/main/java/com/example/android/sunshine/app/ForecastFragment.java index b2284abd6..cf90ce406 100644 --- a/app/src/main/java/com/example/android/sunshine/app/ForecastFragment.java +++ b/app/src/main/java/com/example/android/sunshine/app/ForecastFragment.java @@ -16,11 +16,13 @@ package com.example.android.sunshine.app; import android.content.Intent; +import android.database.Cursor; import android.net.Uri; -import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.Fragment; -import android.text.format.Time; +import android.support.v4.app.LoaderManager; +import android.support.v4.content.CursorLoader; +import android.support.v4.content.Loader; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; @@ -29,30 +31,68 @@ import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; -import android.widget.ArrayAdapter; import android.widget.ListView; -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.URL; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import com.example.android.sunshine.app.data.WeatherContract; +import com.example.android.sunshine.app.sync.SunshineSyncAdapter; /** * Encapsulates fetching the forecast and displaying it as a {@link ListView} layout. */ -public class ForecastFragment extends Fragment { - - private ArrayAdapter mForecastAdapter; +public class ForecastFragment extends Fragment implements LoaderManager.LoaderCallbacks { + public static final String LOG_TAG = ForecastFragment.class.getSimpleName(); + private ForecastAdapter mForecastAdapter; + + private ListView mListView; + private int mPosition = ListView.INVALID_POSITION; + private boolean mUseTodayLayout; + + private static final String SELECTED_KEY = "selected_position"; + + private static final int FORECAST_LOADER = 0; + // For the forecast view we're showing only a small subset of the stored data. + // Specify the columns we need. + private static final String[] FORECAST_COLUMNS = { + // In this case the id needs to be fully qualified with a table name, since + // the content provider joins the location & weather tables in the background + // (both have an _id column) + // On the one hand, that's annoying. On the other, you can search the weather table + // using the location set by the user, which is only in the Location table. + // So the convenience is worth it. + WeatherContract.WeatherEntry.TABLE_NAME + "." + WeatherContract.WeatherEntry._ID, + WeatherContract.WeatherEntry.COLUMN_DATE, + WeatherContract.WeatherEntry.COLUMN_SHORT_DESC, + WeatherContract.WeatherEntry.COLUMN_MAX_TEMP, + WeatherContract.WeatherEntry.COLUMN_MIN_TEMP, + WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, + WeatherContract.WeatherEntry.COLUMN_WEATHER_ID, + WeatherContract.LocationEntry.COLUMN_COORD_LAT, + WeatherContract.LocationEntry.COLUMN_COORD_LONG + }; + + // These indices are tied to FORECAST_COLUMNS. If FORECAST_COLUMNS changes, these + // must change. + static final int COL_WEATHER_ID = 0; + static final int COL_WEATHER_DATE = 1; + static final int COL_WEATHER_DESC = 2; + static final int COL_WEATHER_MAX_TEMP = 3; + static final int COL_WEATHER_MIN_TEMP = 4; + static final int COL_LOCATION_SETTING = 5; + static final int COL_WEATHER_CONDITION_ID = 6; + static final int COL_COORD_LAT = 7; + static final int COL_COORD_LONG = 8; + + /** + * A callback interface that all activities containing this fragment must + * implement. This mechanism allows activities to be notified of item + * selections. + */ + public interface Callback { + /** + * DetailFragmentCallback for when an item has been selected. + */ + public void onItemSelected(Uri dateUri); + } public ForecastFragment() { } @@ -75,11 +115,15 @@ public boolean onOptionsItemSelected(MenuItem item) { // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); - if (id == R.id.action_refresh) { - FetchWeatherTask weatherTask = new FetchWeatherTask(); - weatherTask.execute("94043"); +// if (id == R.id.action_refresh) { +// updateWeather(); +// return true; +// } + if (id == R.id.action_map) { + openPreferredLocationInMap(); return true; } + return super.onOptionsItemSelected(item); } @@ -87,250 +131,144 @@ public boolean onOptionsItemSelected(MenuItem item) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - // Create some dummy data for the ListView. Here's a sample weekly forecast - String[] data = { - "Mon 6/23 - Sunny - 31/17", - "Tue 6/24 - Foggy - 21/8", - "Wed 6/25 - Cloudy - 22/17", - "Thurs 6/26 - Rainy - 18/11", - "Fri 6/27 - Foggy - 21/10", - "Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18", - "Sun 6/29 - Sunny - 20/7" - }; - List weekForecast = new ArrayList(Arrays.asList(data)); - - // Now that we have some dummy forecast data, create an ArrayAdapter. - // The ArrayAdapter will take data from a source (like our dummy forecast) and + // The ForecastAdapter will take data from a source and // use it to populate the ListView it's attached to. - mForecastAdapter = - new ArrayAdapter( - getActivity(), // The current context (this activity) - R.layout.list_item_forecast, // The name of the layout ID. - R.id.list_item_forecast_textview, // The ID of the textview to populate. - weekForecast); + mForecastAdapter = new ForecastAdapter(getActivity(), null, 0); View rootView = inflater.inflate(R.layout.fragment_main, container, false); // Get a reference to the ListView, and attach this adapter to it. - ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast); - listView.setAdapter(mForecastAdapter); - listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + mListView = (ListView) rootView.findViewById(R.id.listview_forecast); + mListView.setAdapter(mForecastAdapter); + // We'll call our MainActivity + mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView adapterView, View view, int position, long l) { - String forecast = mForecastAdapter.getItem(position); - Intent intent = new Intent(getActivity(), DetailActivity.class) - .putExtra(Intent.EXTRA_TEXT, forecast); - startActivity(intent); + // CursorAdapter returns a cursor at the correct position for getItem(), or null + // if it cannot seek to that position. + Cursor cursor = (Cursor) adapterView.getItemAtPosition(position); + if (cursor != null) { + String locationSetting = Utility.getPreferredLocation(getActivity()); + ((Callback) getActivity()) + .onItemSelected(WeatherContract.WeatherEntry.buildWeatherLocationWithDate( + locationSetting, cursor.getLong(COL_WEATHER_DATE) + )); + } + mPosition = position; } }); - return rootView; - } + // If there's instance state, mine it for useful information. + // The end-goal here is that the user never knows that turning their device sideways + // does crazy lifecycle related things. It should feel like some stuff stretched out, + // or magically appeared to take advantage of room, but data or place in the app was never + // actually *lost*. + if (savedInstanceState != null && savedInstanceState.containsKey(SELECTED_KEY)) { + // The listview probably hasn't even been populated yet. Actually perform the + // swapout in onLoadFinished. + mPosition = savedInstanceState.getInt(SELECTED_KEY); + } - public class FetchWeatherTask extends AsyncTask { + mForecastAdapter.setUseTodayLayout(mUseTodayLayout); - private final String LOG_TAG = FetchWeatherTask.class.getSimpleName(); + return rootView; + } - /* The date/time conversion code is going to be moved outside the asynctask later, - * so for convenience we're breaking it out into its own method now. - */ - private String getReadableDateString(long time){ - // Because the API returns a unix timestamp (measured in seconds), - // it must be converted to milliseconds in order to be converted to valid date. - SimpleDateFormat shortenedDateFormat = new SimpleDateFormat("EEE MMM dd"); - return shortenedDateFormat.format(time); - } + @Override + public void onActivityCreated(Bundle savedInstanceState) { + getLoaderManager().initLoader(FORECAST_LOADER, null, this); + super.onActivityCreated(savedInstanceState); + } - /** - * Prepare the weather high/lows for presentation. - */ - private String formatHighLows(double high, double low) { - // For presentation, assume the user doesn't care about tenths of a degree. - long roundedHigh = Math.round(high); - long roundedLow = Math.round(low); + // since we read the location when we create the loader, all we need to do is restart things + void onLocationChanged( ) { + updateWeather(); + getLoaderManager().restartLoader(FORECAST_LOADER, null, this); + } - String highLowStr = roundedHigh + "/" + roundedLow; - return highLowStr; - } + private void updateWeather() { + SunshineSyncAdapter.syncImmediately(getActivity()); + } - /** - * Take the String representing the complete forecast in JSON Format and - * pull out the data we need to construct the Strings needed for the wireframes. - * - * Fortunately parsing is easy: constructor takes the JSON string and converts it - * into an Object hierarchy for us. - */ - private String[] getWeatherDataFromJson(String forecastJsonStr, int numDays) - throws JSONException { - - // These are the names of the JSON objects that need to be extracted. - final String OWM_LIST = "list"; - final String OWM_WEATHER = "weather"; - final String OWM_TEMPERATURE = "temp"; - final String OWM_MAX = "max"; - final String OWM_MIN = "min"; - final String OWM_DESCRIPTION = "main"; - - JSONObject forecastJson = new JSONObject(forecastJsonStr); - JSONArray weatherArray = forecastJson.getJSONArray(OWM_LIST); - - // OWM returns daily forecasts based upon the local time of the city that is being - // asked for, which means that we need to know the GMT offset to translate this data - // properly. - - // Since this data is also sent in-order and the first day is always the - // current day, we're going to take advantage of that to get a nice - // normalized UTC date for all of our weather. - - Time dayTime = new Time(); - dayTime.setToNow(); - - // we start at the day returned by local time. Otherwise this is a mess. - int julianStartDay = Time.getJulianDay(System.currentTimeMillis(), dayTime.gmtoff); - - // now we work exclusively in UTC - dayTime = new Time(); - - String[] resultStrs = new String[numDays]; - for(int i = 0; i < weatherArray.length(); i++) { - // For now, using the format "Day, description, hi/low" - String day; - String description; - String highAndLow; - - // Get the JSON object representing the day - JSONObject dayForecast = weatherArray.getJSONObject(i); - - // The date/time is returned as a long. We need to convert that - // into something human-readable, since most people won't read "1400356800" as - // "this saturday". - long dateTime; - // Cheating to convert this to UTC time, which is what we want anyhow - dateTime = dayTime.setJulianDay(julianStartDay+i); - day = getReadableDateString(dateTime); - - // description is in a child array called "weather", which is 1 element long. - JSONObject weatherObject = dayForecast.getJSONArray(OWM_WEATHER).getJSONObject(0); - description = weatherObject.getString(OWM_DESCRIPTION); - - // Temperatures are in a child object called "temp". Try not to name variables - // "temp" when working with temperature. It confuses everybody. - JSONObject temperatureObject = dayForecast.getJSONObject(OWM_TEMPERATURE); - double high = temperatureObject.getDouble(OWM_MAX); - double low = temperatureObject.getDouble(OWM_MIN); - - highAndLow = formatHighLows(high, low); - resultStrs[i] = day + " - " + description + " - " + highAndLow; + private void openPreferredLocationInMap() { + // Using the URI scheme for showing a location found on a map. This super-handy + // intent can is detailed in the "Common Intents" page of Android's developer site: + // http://developer.android.com/guide/components/intents-common.html#Maps + if ( null != mForecastAdapter ) { + Cursor c = mForecastAdapter.getCursor(); + if ( null != c ) { + c.moveToPosition(0); + String posLat = c.getString(COL_COORD_LAT); + String posLong = c.getString(COL_COORD_LONG); + Uri geoLocation = Uri.parse("geo:" + posLat + "," + posLong); + + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setData(geoLocation); + + if (intent.resolveActivity(getActivity().getPackageManager()) != null) { + startActivity(intent); + } else { + Log.d(LOG_TAG, "Couldn't call " + geoLocation.toString() + ", no receiving apps installed!"); + } } - return resultStrs; } - @Override - protected String[] doInBackground(String... params) { - - // If there's no zip code, there's nothing to look up. Verify size of params. - if (params.length == 0) { - return null; - } - - // These two need to be declared outside the try/catch - // so that they can be closed in the finally block. - HttpURLConnection urlConnection = null; - BufferedReader reader = null; - - // Will contain the raw JSON response as a string. - String forecastJsonStr = null; - - String format = "json"; - String units = "metric"; - int numDays = 7; - - try { - // Construct the URL for the OpenWeatherMap query - // Possible parameters are avaiable at OWM's forecast API page, at - // http://openweathermap.org/API#forecast - final String FORECAST_BASE_URL = - "http://api.openweathermap.org/data/2.5/forecast/daily?"; - final String QUERY_PARAM = "q"; - final String FORMAT_PARAM = "mode"; - final String UNITS_PARAM = "units"; - final String DAYS_PARAM = "cnt"; - - Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon() - .appendQueryParameter(QUERY_PARAM, params[0]) - .appendQueryParameter(FORMAT_PARAM, format) - .appendQueryParameter(UNITS_PARAM, units) - .appendQueryParameter(DAYS_PARAM, Integer.toString(numDays)) - .build(); - - URL url = new URL(builtUri.toString()); - - // Create the request to OpenWeatherMap, and open the connection - urlConnection = (HttpURLConnection) url.openConnection(); - urlConnection.setRequestMethod("GET"); - urlConnection.connect(); - - // Read the input stream into a String - InputStream inputStream = urlConnection.getInputStream(); - StringBuffer buffer = new StringBuffer(); - if (inputStream == null) { - // Nothing to do. - return null; - } - reader = new BufferedReader(new InputStreamReader(inputStream)); - - String line; - while ((line = reader.readLine()) != null) { - // Since it's JSON, adding a newline isn't necessary (it won't affect parsing) - // But it does make debugging a *lot* easier if you print out the completed - // buffer for debugging. - buffer.append(line + "\n"); - } + } - if (buffer.length() == 0) { - // Stream was empty. No point in parsing. - return null; - } - forecastJsonStr = buffer.toString(); - } catch (IOException e) { - Log.e(LOG_TAG, "Error ", e); - // If the code didn't successfully get the weather data, there's no point in attemping - // to parse it. - return null; - } finally { - if (urlConnection != null) { - urlConnection.disconnect(); - } - if (reader != null) { - try { - reader.close(); - } catch (final IOException e) { - Log.e(LOG_TAG, "Error closing stream", e); - } - } - } + @Override + public void onSaveInstanceState(Bundle outState) { + // When tablets rotate, the currently selected list item needs to be saved. + // When no item is selected, mPosition will be set to Listview.INVALID_POSITION, + // so check for that before storing. + if (mPosition != ListView.INVALID_POSITION) { + outState.putInt(SELECTED_KEY, mPosition); + } + super.onSaveInstanceState(outState); + } - try { - return getWeatherDataFromJson(forecastJsonStr, numDays); - } catch (JSONException e) { - Log.e(LOG_TAG, e.getMessage(), e); - e.printStackTrace(); - } + @Override + public Loader onCreateLoader(int i, Bundle bundle) { + // This is called when a new Loader needs to be created. This + // fragment only uses one loader, so we don't care about checking the id. + + // To only show current and future dates, filter the query to return weather only for + // dates after or including today. + + // Sort order: Ascending, by date. + String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + " ASC"; + + String locationSetting = Utility.getPreferredLocation(getActivity()); + Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate( + locationSetting, System.currentTimeMillis()); + + return new CursorLoader(getActivity(), + weatherForLocationUri, + FORECAST_COLUMNS, + null, + null, + sortOrder); + } - // This will only happen if there was an error getting or parsing the forecast. - return null; + @Override + public void onLoadFinished(Loader loader, Cursor data) { + mForecastAdapter.swapCursor(data); + if (mPosition != ListView.INVALID_POSITION) { + // If we don't need to restart the loader, and there's a desired position to restore + // to, do so now. + mListView.smoothScrollToPosition(mPosition); } + } - @Override - protected void onPostExecute(String[] result) { - if (result != null) { - mForecastAdapter.clear(); - for(String dayForecastStr : result) { - mForecastAdapter.add(dayForecastStr); - } - // New data is back from the server. Hooray! - } + @Override + public void onLoaderReset(Loader loader) { + mForecastAdapter.swapCursor(null); + } + + public void setUseTodayLayout(boolean useTodayLayout) { + mUseTodayLayout = useTodayLayout; + if (mForecastAdapter != null) { + mForecastAdapter.setUseTodayLayout(mUseTodayLayout); } } -} +} \ No newline at end of file diff --git a/app/src/main/java/com/example/android/sunshine/app/MainActivity.java b/app/src/main/java/com/example/android/sunshine/app/MainActivity.java index e8729e77d..5337580c2 100644 --- a/app/src/main/java/com/example/android/sunshine/app/MainActivity.java +++ b/app/src/main/java/com/example/android/sunshine/app/MainActivity.java @@ -15,22 +15,52 @@ */ package com.example.android.sunshine.app; +import android.content.Intent; +import android.net.Uri; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; -public class MainActivity extends ActionBarActivity { +import com.example.android.sunshine.app.sync.SunshineSyncAdapter; + +public class MainActivity extends ActionBarActivity implements ForecastFragment.Callback { + + private final String LOG_TAG = MainActivity.class.getSimpleName(); + private static final String DETAILFRAGMENT_TAG = "DFTAG"; + + private boolean mTwoPane; + private String mLocation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + mLocation = Utility.getPreferredLocation(this); + setContentView(R.layout.activity_main); - if (savedInstanceState == null) { - getSupportFragmentManager().beginTransaction() - .add(R.id.container, new ForecastFragment()) - .commit(); + if (findViewById(R.id.weather_detail_container) != null) { + // The detail container view will be present only in the large-screen layouts + // (res/layout-sw600dp). If this view is present, then the activity should be + // in two-pane mode. + mTwoPane = true; + // In two-pane mode, show the detail view in this activity by + // adding or replacing the detail fragment using a + // fragment transaction. + if (savedInstanceState == null) { + getSupportFragmentManager().beginTransaction() + .replace(R.id.weather_detail_container, new DetailFragment(), DETAILFRAGMENT_TAG) + .commit(); + } + } else { + mTwoPane = false; + getSupportActionBar().setElevation(0f); } + + ForecastFragment forecastFragment = ((ForecastFragment)getSupportFragmentManager() + .findFragmentById(R.id.fragment_forecast)); + forecastFragment.setUseTodayLayout(!mTwoPane); + + SunshineSyncAdapter.initializeSyncAdapter(this); } @Override @@ -49,9 +79,50 @@ public boolean onOptionsItemSelected(MenuItem item) { //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { + startActivity(new Intent(this, SettingsActivity.class)); return true; } return super.onOptionsItemSelected(item); } + + @Override + protected void onResume() { + super.onResume(); + String location = Utility.getPreferredLocation( this ); + // update the location in our second pane using the fragment manager + if (location != null && !location.equals(mLocation)) { + ForecastFragment ff = (ForecastFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_forecast); + if ( null != ff ) { + ff.onLocationChanged(); + } + DetailFragment df = (DetailFragment)getSupportFragmentManager().findFragmentByTag(DETAILFRAGMENT_TAG); + if ( null != df ) { + df.onLocationChanged(location); + } + mLocation = location; + } + } + + @Override + public void onItemSelected(Uri contentUri) { + if (mTwoPane) { + // In two-pane mode, show the detail view in this activity by + // adding or replacing the detail fragment using a + // fragment transaction. + Bundle args = new Bundle(); + args.putParcelable(DetailFragment.DETAIL_URI, contentUri); + + DetailFragment fragment = new DetailFragment(); + fragment.setArguments(args); + + getSupportFragmentManager().beginTransaction() + .replace(R.id.weather_detail_container, fragment, DETAILFRAGMENT_TAG) + .commit(); + } else { + Intent intent = new Intent(this, DetailActivity.class) + .setData(contentUri); + startActivity(intent); + } + } } diff --git a/app/src/main/java/com/example/android/sunshine/app/SettingsActivity.java b/app/src/main/java/com/example/android/sunshine/app/SettingsActivity.java new file mode 100644 index 000000000..4ac02939b --- /dev/null +++ b/app/src/main/java/com/example/android/sunshine/app/SettingsActivity.java @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.sunshine.app; + +import android.annotation.TargetApi; +import android.content.Intent; +import android.os.Build; +import android.os.Bundle; +import android.preference.ListPreference; +import android.preference.Preference; +import android.preference.PreferenceActivity; +import android.preference.PreferenceManager; + +/** + * A {@link PreferenceActivity} that presents a set of application settings. + *

+ * See + * Android Design: Settings for design guidelines and the Settings + * API Guide for more information on developing a Settings UI. + */ +public class SettingsActivity extends PreferenceActivity + implements Preference.OnPreferenceChangeListener { + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + // Add 'general' preferences, defined in the XML file + addPreferencesFromResource(R.xml.pref_general); + + // For all preferences, attach an OnPreferenceChangeListener so the UI summary can be + // updated when the preference changes. + bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key))); + bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_units_key))); + } + + /** + * Attaches a listener so the summary is always updated with the preference value. + * Also fires the listener once, to initialize the summary (so it shows up before the value + * is changed.) + */ + private void bindPreferenceSummaryToValue(Preference preference) { + // Set the listener to watch for value changes. + preference.setOnPreferenceChangeListener(this); + + // Trigger the listener immediately with the preference's + // current value. + onPreferenceChange(preference, + PreferenceManager + .getDefaultSharedPreferences(preference.getContext()) + .getString(preference.getKey(), "")); + } + + @Override + public boolean onPreferenceChange(Preference preference, Object value) { + String stringValue = value.toString(); + + if (preference instanceof ListPreference) { + // For list preferences, look up the correct display value in + // the preference's 'entries' list (since they have separate labels/values). + ListPreference listPreference = (ListPreference) preference; + int prefIndex = listPreference.findIndexOfValue(stringValue); + if (prefIndex >= 0) { + preference.setSummary(listPreference.getEntries()[prefIndex]); + } + } else { + // For other preferences, set the summary to the value's simple string representation. + preference.setSummary(stringValue); + } + return true; + } + + @TargetApi(Build.VERSION_CODES.JELLY_BEAN) + @Override + public Intent getParentActivityIntent() { + return super.getParentActivityIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/android/sunshine/app/Utility.java b/app/src/main/java/com/example/android/sunshine/app/Utility.java new file mode 100644 index 000000000..664a523aa --- /dev/null +++ b/app/src/main/java/com/example/android/sunshine/app/Utility.java @@ -0,0 +1,249 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.sunshine.app; + +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; +import android.text.format.Time; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class Utility { + public static String getPreferredLocation(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + return prefs.getString(context.getString(R.string.pref_location_key), + context.getString(R.string.pref_location_default)); + } + + public static boolean isMetric(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + return prefs.getString(context.getString(R.string.pref_units_key), + context.getString(R.string.pref_units_metric)) + .equals(context.getString(R.string.pref_units_metric)); + } + + public static String formatTemperature(Context context, double temperature) { + // Data stored in Celsius by default. If user prefers to see in Fahrenheit, convert + // the values here. + String suffix = "\u00B0"; + if (!isMetric(context)) { + temperature = (temperature * 1.8) + 32; + } + + // For presentation, assume the user doesn't care about tenths of a degree. + return String.format(context.getString(R.string.format_temperature), temperature); + } + + static String formatDate(long dateInMilliseconds) { + Date date = new Date(dateInMilliseconds); + return DateFormat.getDateInstance().format(date); + } + + // Format used for storing dates in the database. ALso used for converting those strings + // back into date objects for comparison/processing. + public static final String DATE_FORMAT = "yyyyMMdd"; + + /** + * Helper method to convert the database representation of the date into something to display + * to users. As classy and polished a user experience as "20140102" is, we can do better. + * + * @param context Context to use for resource localization + * @param dateInMillis The date in milliseconds + * @return a user-friendly representation of the date. + */ + public static String getFriendlyDayString(Context context, long dateInMillis) { + // The day string for forecast uses the following logic: + // For today: "Today, June 8" + // For tomorrow: "Tomorrow" + // For the next 5 days: "Wednesday" (just the day name) + // For all days after that: "Mon Jun 8" + + Time time = new Time(); + time.setToNow(); + long currentTime = System.currentTimeMillis(); + int julianDay = Time.getJulianDay(dateInMillis, time.gmtoff); + int currentJulianDay = Time.getJulianDay(currentTime, time.gmtoff); + + // If the date we're building the String for is today's date, the format + // is "Today, June 24" + if (julianDay == currentJulianDay) { + String today = context.getString(R.string.today); + int formatId = R.string.format_full_friendly_date; + return String.format(context.getString( + formatId, + today, + getFormattedMonthDay(context, dateInMillis))); + } else if ( julianDay < currentJulianDay + 7 ) { + // If the input date is less than a week in the future, just return the day name. + return getDayName(context, dateInMillis); + } else { + // Otherwise, use the form "Mon Jun 3" + SimpleDateFormat shortenedDateFormat = new SimpleDateFormat("EEE MMM dd"); + return shortenedDateFormat.format(dateInMillis); + } + } + + /** + * Given a day, returns just the name to use for that day. + * E.g "today", "tomorrow", "wednesday". + * + * @param context Context to use for resource localization + * @param dateInMillis The date in milliseconds + * @return + */ + public static String getDayName(Context context, long dateInMillis) { + // If the date is today, return the localized version of "Today" instead of the actual + // day name. + + Time t = new Time(); + t.setToNow(); + int julianDay = Time.getJulianDay(dateInMillis, t.gmtoff); + int currentJulianDay = Time.getJulianDay(System.currentTimeMillis(), t.gmtoff); + if (julianDay == currentJulianDay) { + return context.getString(R.string.today); + } else if ( julianDay == currentJulianDay +1 ) { + return context.getString(R.string.tomorrow); + } else { + Time time = new Time(); + time.setToNow(); + // Otherwise, the format is just the day of the week (e.g "Wednesday". + SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE"); + return dayFormat.format(dateInMillis); + } + } + + /** + * Converts db date format to the format "Month day", e.g "June 24". + * @param context Context to use for resource localization + * @param dateInMillis The db formatted date string, expected to be of the form specified + * in Utility.DATE_FORMAT + * @return The day in the form of a string formatted "December 6" + */ + public static String getFormattedMonthDay(Context context, long dateInMillis ) { + Time time = new Time(); + time.setToNow(); + SimpleDateFormat dbDateFormat = new SimpleDateFormat(Utility.DATE_FORMAT); + SimpleDateFormat monthDayFormat = new SimpleDateFormat("MMMM dd"); + String monthDayString = monthDayFormat.format(dateInMillis); + return monthDayString; + } + + public static String getFormattedWind(Context context, float windSpeed, float degrees) { + int windFormat; + if (Utility.isMetric(context)) { + windFormat = R.string.format_wind_kmh; + } else { + windFormat = R.string.format_wind_mph; + windSpeed = .621371192237334f * windSpeed; + } + + // From wind direction in degrees, determine compass direction as a string (e.g NW) + // You know what's fun, writing really long if/else statements with tons of possible + // conditions. Seriously, try it! + String direction = "Unknown"; + if (degrees >= 337.5 || degrees < 22.5) { + direction = "N"; + } else if (degrees >= 22.5 && degrees < 67.5) { + direction = "NE"; + } else if (degrees >= 67.5 && degrees < 112.5) { + direction = "E"; + } else if (degrees >= 112.5 && degrees < 157.5) { + direction = "SE"; + } else if (degrees >= 157.5 && degrees < 202.5) { + direction = "S"; + } else if (degrees >= 202.5 && degrees < 247.5) { + direction = "SW"; + } else if (degrees >= 247.5 && degrees < 292.5) { + direction = "W"; + } else if (degrees >= 292.5 && degrees < 337.5) { + direction = "NW"; + } + return String.format(context.getString(windFormat), windSpeed, direction); + } + + /** + * Helper method to provide the icon resource id according to the weather condition id returned + * by the OpenWeatherMap call. + * @param weatherId from OpenWeatherMap API response + * @return resource id for the corresponding icon. -1 if no relation is found. + */ + public static int getIconResourceForWeatherCondition(int weatherId) { + // Based on weather code data found at: + // http://bugs.openweathermap.org/projects/api/wiki/Weather_Condition_Codes + if (weatherId >= 200 && weatherId <= 232) { + return R.drawable.ic_storm; + } else if (weatherId >= 300 && weatherId <= 321) { + return R.drawable.ic_light_rain; + } else if (weatherId >= 500 && weatherId <= 504) { + return R.drawable.ic_rain; + } else if (weatherId == 511) { + return R.drawable.ic_snow; + } else if (weatherId >= 520 && weatherId <= 531) { + return R.drawable.ic_rain; + } else if (weatherId >= 600 && weatherId <= 622) { + return R.drawable.ic_snow; + } else if (weatherId >= 701 && weatherId <= 761) { + return R.drawable.ic_fog; + } else if (weatherId == 761 || weatherId == 781) { + return R.drawable.ic_storm; + } else if (weatherId == 800) { + return R.drawable.ic_clear; + } else if (weatherId == 801) { + return R.drawable.ic_light_clouds; + } else if (weatherId >= 802 && weatherId <= 804) { + return R.drawable.ic_cloudy; + } + return -1; + } + + /** + * Helper method to provide the art resource id according to the weather condition id returned + * by the OpenWeatherMap call. + * @param weatherId from OpenWeatherMap API response + * @return resource id for the corresponding icon. -1 if no relation is found. + */ + public static int getArtResourceForWeatherCondition(int weatherId) { + // Based on weather code data found at: + // http://bugs.openweathermap.org/projects/api/wiki/Weather_Condition_Codes + if (weatherId >= 200 && weatherId <= 232) { + return R.drawable.art_storm; + } else if (weatherId >= 300 && weatherId <= 321) { + return R.drawable.art_light_rain; + } else if (weatherId >= 500 && weatherId <= 504) { + return R.drawable.art_rain; + } else if (weatherId == 511) { + return R.drawable.art_snow; + } else if (weatherId >= 520 && weatherId <= 531) { + return R.drawable.art_rain; + } else if (weatherId >= 600 && weatherId <= 622) { + return R.drawable.art_snow; + } else if (weatherId >= 701 && weatherId <= 761) { + return R.drawable.art_fog; + } else if (weatherId == 761 || weatherId == 781) { + return R.drawable.art_storm; + } else if (weatherId == 800) { + return R.drawable.art_clear; + } else if (weatherId == 801) { + return R.drawable.art_light_clouds; + } else if (weatherId >= 802 && weatherId <= 804) { + return R.drawable.art_clouds; + } + return -1; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/android/sunshine/app/data/WeatherContract.java b/app/src/main/java/com/example/android/sunshine/app/data/WeatherContract.java new file mode 100644 index 000000000..63387c4a8 --- /dev/null +++ b/app/src/main/java/com/example/android/sunshine/app/data/WeatherContract.java @@ -0,0 +1,168 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.sunshine.app.data; + +import android.content.ContentResolver; +import android.content.ContentUris; +import android.net.Uri; +import android.provider.BaseColumns; +import android.text.format.Time; + +/** + * Defines table and column names for the weather database. + */ +public class WeatherContract { + + // The "Content authority" is a name for the entire content provider, similar to the + // relationship between a domain name and its website. A convenient string to use for the + // content authority is the package name for the app, which is guaranteed to be unique on the + // device. + public static final String CONTENT_AUTHORITY = "com.example.android.sunshine.app"; + + // Use CONTENT_AUTHORITY to create the base of all URI's which apps will use to contact + // the content provider. + public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY); + + // Possible paths (appended to base content URI for possible URI's) + // For instance, content://com.example.android.sunshine.app/weather/ is a valid path for + // looking at weather data. content://com.example.android.sunshine.app/givemeroot/ will fail, + // as the ContentProvider hasn't been given any information on what to do with "givemeroot". + // At least, let's hope not. Don't be that dev, reader. Don't be that dev. + public static final String PATH_WEATHER = "weather"; + public static final String PATH_LOCATION = "location"; + + // To make it easy to query for the exact date, we normalize all dates that go into + // the database to the start of the the Julian day at UTC. + public static long normalizeDate(long startDate) { + // normalize the start date to the beginning of the (UTC) day + Time time = new Time(); + time.set(startDate); + int julianDay = Time.getJulianDay(startDate, time.gmtoff); + return time.setJulianDay(julianDay); + } + + /* Inner class that defines the table contents of the location table */ + public static final class LocationEntry implements BaseColumns { + + public static final Uri CONTENT_URI = + BASE_CONTENT_URI.buildUpon().appendPath(PATH_LOCATION).build(); + + public static final String CONTENT_TYPE = + ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_LOCATION; + public static final String CONTENT_ITEM_TYPE = + ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_LOCATION; + + // Table name + public static final String TABLE_NAME = "location"; + + // The location setting string is what will be sent to openweathermap + // as the location query. + public static final String COLUMN_LOCATION_SETTING = "location_setting"; + + // Human readable location string, provided by the API. Because for styling, + // "Mountain View" is more recognizable than 94043. + public static final String COLUMN_CITY_NAME = "city_name"; + + // In order to uniquely pinpoint the location on the map when we launch the + // map intent, we store the latitude and longitude as returned by openweathermap. + public static final String COLUMN_COORD_LAT = "coord_lat"; + public static final String COLUMN_COORD_LONG = "coord_long"; + + public static Uri buildLocationUri(long id) { + return ContentUris.withAppendedId(CONTENT_URI, id); + } + } + + /* Inner class that defines the table contents of the weather table */ + public static final class WeatherEntry implements BaseColumns { + + public static final Uri CONTENT_URI = + BASE_CONTENT_URI.buildUpon().appendPath(PATH_WEATHER).build(); + + public static final String CONTENT_TYPE = + ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_WEATHER; + public static final String CONTENT_ITEM_TYPE = + ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_WEATHER; + + public static final String TABLE_NAME = "weather"; + + // Column with the foreign key into the location table. + public static final String COLUMN_LOC_KEY = "location_id"; + // Date, stored as long in milliseconds since the epoch + public static final String COLUMN_DATE = "date"; + // Weather id as returned by API, to identify the icon to be used + public static final String COLUMN_WEATHER_ID = "weather_id"; + + // Short description and long description of the weather, as provided by API. + // e.g "clear" vs "sky is clear". + public static final String COLUMN_SHORT_DESC = "short_desc"; + + // Min and max temperatures for the day (stored as floats) + public static final String COLUMN_MIN_TEMP = "min"; + public static final String COLUMN_MAX_TEMP = "max"; + + // Humidity is stored as a float representing percentage + public static final String COLUMN_HUMIDITY = "humidity"; + + // Humidity is stored as a float representing percentage + public static final String COLUMN_PRESSURE = "pressure"; + + // Windspeed is stored as a float representing windspeed mph + public static final String COLUMN_WIND_SPEED = "wind"; + + // Degrees are meteorological degrees (e.g, 0 is north, 180 is south). Stored as floats. + public static final String COLUMN_DEGREES = "degrees"; + + public static Uri buildWeatherUri(long id) { + return ContentUris.withAppendedId(CONTENT_URI, id); + } + + /* + Student: This is the buildWeatherLocation function you filled in. + */ + public static Uri buildWeatherLocation(String locationSetting) { + return CONTENT_URI.buildUpon().appendPath(locationSetting).build(); + } + + public static Uri buildWeatherLocationWithStartDate( + String locationSetting, long startDate) { + long normalizedDate = normalizeDate(startDate); + return CONTENT_URI.buildUpon().appendPath(locationSetting) + .appendQueryParameter(COLUMN_DATE, Long.toString(normalizedDate)).build(); + } + + public static Uri buildWeatherLocationWithDate(String locationSetting, long date) { + return CONTENT_URI.buildUpon().appendPath(locationSetting) + .appendPath(Long.toString(normalizeDate(date))).build(); + } + + public static String getLocationSettingFromUri(Uri uri) { + return uri.getPathSegments().get(1); + } + + public static long getDateFromUri(Uri uri) { + return Long.parseLong(uri.getPathSegments().get(2)); + } + + public static long getStartDateFromUri(Uri uri) { + String dateString = uri.getQueryParameter(COLUMN_DATE); + if (null != dateString && dateString.length() > 0) + return Long.parseLong(dateString); + else + return 0; + } + } +} diff --git a/app/src/main/java/com/example/android/sunshine/app/data/WeatherDbHelper.java b/app/src/main/java/com/example/android/sunshine/app/data/WeatherDbHelper.java new file mode 100644 index 000000000..a933c101f --- /dev/null +++ b/app/src/main/java/com/example/android/sunshine/app/data/WeatherDbHelper.java @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.sunshine.app.data; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; + +import com.example.android.sunshine.app.data.WeatherContract.LocationEntry; +import com.example.android.sunshine.app.data.WeatherContract.WeatherEntry; + +/** + * Manages a local database for weather data. + */ +public class WeatherDbHelper extends SQLiteOpenHelper { + + // If you change the database schema, you must increment the database version. + private static final int DATABASE_VERSION = 2; + + static final String DATABASE_NAME = "weather.db"; + + public WeatherDbHelper(Context context) { + super(context, DATABASE_NAME, null, DATABASE_VERSION); + } + + @Override + public void onCreate(SQLiteDatabase sqLiteDatabase) { + // Create a table to hold locations. A location consists of the string supplied in the + // location setting, the city name, and the latitude and longitude + final String SQL_CREATE_LOCATION_TABLE = "CREATE TABLE " + LocationEntry.TABLE_NAME + " (" + + LocationEntry._ID + " INTEGER PRIMARY KEY," + + LocationEntry.COLUMN_LOCATION_SETTING + " TEXT UNIQUE NOT NULL, " + + LocationEntry.COLUMN_CITY_NAME + " TEXT NOT NULL, " + + LocationEntry.COLUMN_COORD_LAT + " REAL NOT NULL, " + + LocationEntry.COLUMN_COORD_LONG + " REAL NOT NULL " + + " );"; + + final String SQL_CREATE_WEATHER_TABLE = "CREATE TABLE " + WeatherEntry.TABLE_NAME + " (" + + // Why AutoIncrement here, and not above? + // Unique keys will be auto-generated in either case. But for weather + // forecasting, it's reasonable to assume the user will want information + // for a certain date and all dates *following*, so the forecast data + // should be sorted accordingly. + WeatherEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + + + // the ID of the location entry associated with this weather data + WeatherEntry.COLUMN_LOC_KEY + " INTEGER NOT NULL, " + + WeatherEntry.COLUMN_DATE + " INTEGER NOT NULL, " + + WeatherEntry.COLUMN_SHORT_DESC + " TEXT NOT NULL, " + + WeatherEntry.COLUMN_WEATHER_ID + " INTEGER NOT NULL," + + + WeatherEntry.COLUMN_MIN_TEMP + " REAL NOT NULL, " + + WeatherEntry.COLUMN_MAX_TEMP + " REAL NOT NULL, " + + + WeatherEntry.COLUMN_HUMIDITY + " REAL NOT NULL, " + + WeatherEntry.COLUMN_PRESSURE + " REAL NOT NULL, " + + WeatherEntry.COLUMN_WIND_SPEED + " REAL NOT NULL, " + + WeatherEntry.COLUMN_DEGREES + " REAL NOT NULL, " + + + // Set up the location column as a foreign key to location table. + " FOREIGN KEY (" + WeatherEntry.COLUMN_LOC_KEY + ") REFERENCES " + + LocationEntry.TABLE_NAME + " (" + LocationEntry._ID + "), " + + + // To assure the application have just one weather entry per day + // per location, it's created a UNIQUE constraint with REPLACE strategy + " UNIQUE (" + WeatherEntry.COLUMN_DATE + ", " + + WeatherEntry.COLUMN_LOC_KEY + ") ON CONFLICT REPLACE);"; + + sqLiteDatabase.execSQL(SQL_CREATE_LOCATION_TABLE); + sqLiteDatabase.execSQL(SQL_CREATE_WEATHER_TABLE); + } + + @Override + public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { + // This database is only a cache for online data, so its upgrade policy is + // to simply to discard the data and start over + // Note that this only fires if you change the version number for your database. + // It does NOT depend on the version number for your application. + // If you want to update the schema without wiping data, commenting out the next 2 lines + // should be your top priority before modifying this method. + sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + LocationEntry.TABLE_NAME); + sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + WeatherEntry.TABLE_NAME); + onCreate(sqLiteDatabase); + } +} diff --git a/app/src/main/java/com/example/android/sunshine/app/data/WeatherProvider.java b/app/src/main/java/com/example/android/sunshine/app/data/WeatherProvider.java new file mode 100644 index 000000000..f607ec793 --- /dev/null +++ b/app/src/main/java/com/example/android/sunshine/app/data/WeatherProvider.java @@ -0,0 +1,354 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.sunshine.app.data; + +import android.annotation.TargetApi; +import android.content.ContentProvider; +import android.content.ContentValues; +import android.content.UriMatcher; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteQueryBuilder; +import android.net.Uri; + +public class WeatherProvider extends ContentProvider { + + // The URI Matcher used by this content provider. + private static final UriMatcher sUriMatcher = buildUriMatcher(); + private WeatherDbHelper mOpenHelper; + + static final int WEATHER = 100; + static final int WEATHER_WITH_LOCATION = 101; + static final int WEATHER_WITH_LOCATION_AND_DATE = 102; + static final int LOCATION = 300; + + private static final SQLiteQueryBuilder sWeatherByLocationSettingQueryBuilder; + + static{ + sWeatherByLocationSettingQueryBuilder = new SQLiteQueryBuilder(); + + //This is an inner join which looks like + //weather INNER JOIN location ON weather.location_id = location._id + sWeatherByLocationSettingQueryBuilder.setTables( + WeatherContract.WeatherEntry.TABLE_NAME + " INNER JOIN " + + WeatherContract.LocationEntry.TABLE_NAME + + " ON " + WeatherContract.WeatherEntry.TABLE_NAME + + "." + WeatherContract.WeatherEntry.COLUMN_LOC_KEY + + " = " + WeatherContract.LocationEntry.TABLE_NAME + + "." + WeatherContract.LocationEntry._ID); + } + + //location.location_setting = ? + private static final String sLocationSettingSelection = + WeatherContract.LocationEntry.TABLE_NAME+ + "." + WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ? "; + + //location.location_setting = ? AND date >= ? + private static final String sLocationSettingWithStartDateSelection = + WeatherContract.LocationEntry.TABLE_NAME+ + "." + WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ? AND " + + WeatherContract.WeatherEntry.COLUMN_DATE + " >= ? "; + + //location.location_setting = ? AND date = ? + private static final String sLocationSettingAndDaySelection = + WeatherContract.LocationEntry.TABLE_NAME + + "." + WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ? AND " + + WeatherContract.WeatherEntry.COLUMN_DATE + " = ? "; + + private Cursor getWeatherByLocationSetting(Uri uri, String[] projection, String sortOrder) { + String locationSetting = WeatherContract.WeatherEntry.getLocationSettingFromUri(uri); + long startDate = WeatherContract.WeatherEntry.getStartDateFromUri(uri); + + String[] selectionArgs; + String selection; + + if (startDate == 0) { + selection = sLocationSettingSelection; + selectionArgs = new String[]{locationSetting}; + } else { + selectionArgs = new String[]{locationSetting, Long.toString(startDate)}; + selection = sLocationSettingWithStartDateSelection; + } + + return sWeatherByLocationSettingQueryBuilder.query(mOpenHelper.getReadableDatabase(), + projection, + selection, + selectionArgs, + null, + null, + sortOrder + ); + } + + private Cursor getWeatherByLocationSettingAndDate( + Uri uri, String[] projection, String sortOrder) { + String locationSetting = WeatherContract.WeatherEntry.getLocationSettingFromUri(uri); + long date = WeatherContract.WeatherEntry.getDateFromUri(uri); + + return sWeatherByLocationSettingQueryBuilder.query(mOpenHelper.getReadableDatabase(), + projection, + sLocationSettingAndDaySelection, + new String[]{locationSetting, Long.toString(date)}, + null, + null, + sortOrder + ); + } + + /* + Students: Here is where you need to create the UriMatcher. This UriMatcher will + match each URI to the WEATHER, WEATHER_WITH_LOCATION, WEATHER_WITH_LOCATION_AND_DATE, + and LOCATION integer constants defined above. You can test this by uncommenting the + testUriMatcher test within TestUriMatcher. + */ + static UriMatcher buildUriMatcher() { + // I know what you're thinking. Why create a UriMatcher when you can use regular + // expressions instead? Because you're not crazy, that's why. + + // All paths added to the UriMatcher have a corresponding code to return when a match is + // found. The code passed into the constructor represents the code to return for the root + // URI. It's common to use NO_MATCH as the code for this case. + final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); + final String authority = WeatherContract.CONTENT_AUTHORITY; + + // For each type of URI you want to add, create a corresponding code. + matcher.addURI(authority, WeatherContract.PATH_WEATHER, WEATHER); + matcher.addURI(authority, WeatherContract.PATH_WEATHER + "/*", WEATHER_WITH_LOCATION); + matcher.addURI(authority, WeatherContract.PATH_WEATHER + "/*/#", WEATHER_WITH_LOCATION_AND_DATE); + + matcher.addURI(authority, WeatherContract.PATH_LOCATION, LOCATION); + return matcher; + } + + /* + Students: We've coded this for you. We just create a new WeatherDbHelper for later use + here. + */ + @Override + public boolean onCreate() { + mOpenHelper = new WeatherDbHelper(getContext()); + return true; + } + + /* + Students: Here's where you'll code the getType function that uses the UriMatcher. You can + test this by uncommenting testGetType in TestProvider. + + */ + @Override + public String getType(Uri uri) { + + // Use the Uri Matcher to determine what kind of URI this is. + final int match = sUriMatcher.match(uri); + + switch (match) { + // Student: Uncomment and fill out these two cases + case WEATHER_WITH_LOCATION_AND_DATE: + return WeatherContract.WeatherEntry.CONTENT_ITEM_TYPE; + case WEATHER_WITH_LOCATION: + return WeatherContract.WeatherEntry.CONTENT_TYPE; + case WEATHER: + return WeatherContract.WeatherEntry.CONTENT_TYPE; + case LOCATION: + return WeatherContract.LocationEntry.CONTENT_TYPE; + default: + throw new UnsupportedOperationException("Unknown uri: " + uri); + } + } + + @Override + public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, + String sortOrder) { + // Here's the switch statement that, given a URI, will determine what kind of request it is, + // and query the database accordingly. + Cursor retCursor; + switch (sUriMatcher.match(uri)) { + // "weather/*/*" + case WEATHER_WITH_LOCATION_AND_DATE: + { + retCursor = getWeatherByLocationSettingAndDate(uri, projection, sortOrder); + break; + } + // "weather/*" + case WEATHER_WITH_LOCATION: { + retCursor = getWeatherByLocationSetting(uri, projection, sortOrder); + break; + } + // "weather" + case WEATHER: { + retCursor = mOpenHelper.getReadableDatabase().query( + WeatherContract.WeatherEntry.TABLE_NAME, + projection, + selection, + selectionArgs, + null, + null, + sortOrder + ); + break; + } + // "location" + case LOCATION: { + retCursor = mOpenHelper.getReadableDatabase().query( + WeatherContract.LocationEntry.TABLE_NAME, + projection, + selection, + selectionArgs, + null, + null, + sortOrder + ); + break; + } + + default: + throw new UnsupportedOperationException("Unknown uri: " + uri); + } + retCursor.setNotificationUri(getContext().getContentResolver(), uri); + return retCursor; + } + + /* + Student: Add the ability to insert Locations to the implementation of this function. + */ + @Override + public Uri insert(Uri uri, ContentValues values) { + final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); + final int match = sUriMatcher.match(uri); + Uri returnUri; + + switch (match) { + case WEATHER: { + normalizeDate(values); + long _id = db.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, values); + if ( _id > 0 ) + returnUri = WeatherContract.WeatherEntry.buildWeatherUri(_id); + else + throw new android.database.SQLException("Failed to insert row into " + uri); + break; + } + case LOCATION: { + long _id = db.insert(WeatherContract.LocationEntry.TABLE_NAME, null, values); + if ( _id > 0 ) + returnUri = WeatherContract.LocationEntry.buildLocationUri(_id); + else + throw new android.database.SQLException("Failed to insert row into " + uri); + break; + } + default: + throw new UnsupportedOperationException("Unknown uri: " + uri); + } + getContext().getContentResolver().notifyChange(uri, null); + return returnUri; + } + + @Override + public int delete(Uri uri, String selection, String[] selectionArgs) { + final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); + final int match = sUriMatcher.match(uri); + int rowsDeleted; + // this makes delete all rows return the number of rows deleted + if ( null == selection ) selection = "1"; + switch (match) { + case WEATHER: + rowsDeleted = db.delete( + WeatherContract.WeatherEntry.TABLE_NAME, selection, selectionArgs); + break; + case LOCATION: + rowsDeleted = db.delete( + WeatherContract.LocationEntry.TABLE_NAME, selection, selectionArgs); + break; + default: + throw new UnsupportedOperationException("Unknown uri: " + uri); + } + // Because a null deletes all rows + if (rowsDeleted != 0) { + getContext().getContentResolver().notifyChange(uri, null); + } + return rowsDeleted; + } + + private void normalizeDate(ContentValues values) { + // normalize the date value + if (values.containsKey(WeatherContract.WeatherEntry.COLUMN_DATE)) { + long dateValue = values.getAsLong(WeatherContract.WeatherEntry.COLUMN_DATE); + values.put(WeatherContract.WeatherEntry.COLUMN_DATE, WeatherContract.normalizeDate(dateValue)); + } + } + + @Override + public int update( + Uri uri, ContentValues values, String selection, String[] selectionArgs) { + final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); + final int match = sUriMatcher.match(uri); + int rowsUpdated; + + switch (match) { + case WEATHER: + normalizeDate(values); + rowsUpdated = db.update(WeatherContract.WeatherEntry.TABLE_NAME, values, selection, + selectionArgs); + break; + case LOCATION: + rowsUpdated = db.update(WeatherContract.LocationEntry.TABLE_NAME, values, selection, + selectionArgs); + break; + default: + throw new UnsupportedOperationException("Unknown uri: " + uri); + } + if (rowsUpdated != 0) { + getContext().getContentResolver().notifyChange(uri, null); + } + return rowsUpdated; + } + + @Override + public int bulkInsert(Uri uri, ContentValues[] values) { + final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); + final int match = sUriMatcher.match(uri); + switch (match) { + case WEATHER: + db.beginTransaction(); + int returnCount = 0; + try { + for (ContentValues value : values) { + normalizeDate(value); + long _id = db.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, value); + if (_id != -1) { + returnCount++; + } + } + db.setTransactionSuccessful(); + } finally { + db.endTransaction(); + } + getContext().getContentResolver().notifyChange(uri, null); + return returnCount; + default: + return super.bulkInsert(uri, values); + } + } + + // You do not need to call this method. This is a method specifically to assist the testing + // framework in running smoothly. You can read more at: + // http://developer.android.com/reference/android/content/ContentProvider.html#shutdown() + @Override + @TargetApi(11) + public void shutdown() { + mOpenHelper.close(); + super.shutdown(); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/android/sunshine/app/sync/SunshineAuthenticator.java b/app/src/main/java/com/example/android/sunshine/app/sync/SunshineAuthenticator.java new file mode 100644 index 000000000..02042b646 --- /dev/null +++ b/app/src/main/java/com/example/android/sunshine/app/sync/SunshineAuthenticator.java @@ -0,0 +1,83 @@ +package com.example.android.sunshine.app.sync; + +import android.accounts.AbstractAccountAuthenticator; +import android.accounts.Account; +import android.accounts.AccountAuthenticatorResponse; +import android.accounts.NetworkErrorException; +import android.content.Context; +import android.os.Bundle; + +/** + * Manages "Authentication" to Sunshine's backend service. The SyncAdapter framework + * requires an authenticator object, so syncing to a service that doesn't need authentication + * typically means creating a stub authenticator like this one. + * This code is copied directly, in its entirety, from + * http://developer.android.com/training/sync-adapters/creating-authenticator.html + * Which is a pretty handy reference when creating your own syncadapters. Just sayin'. + */ +public class SunshineAuthenticator extends AbstractAccountAuthenticator { + + public SunshineAuthenticator(Context context) { + super(context); + } + + // No properties to edit. + @Override + public Bundle editProperties( + AccountAuthenticatorResponse r, String s) { + throw new UnsupportedOperationException(); + } + + // Because we're not actually adding an account to the device, just return null. + @Override + public Bundle addAccount( + AccountAuthenticatorResponse r, + String s, + String s2, + String[] strings, + Bundle bundle) throws NetworkErrorException { + return null; + } + + // Ignore attempts to confirm credentials + @Override + public Bundle confirmCredentials( + AccountAuthenticatorResponse r, + Account account, + Bundle bundle) throws NetworkErrorException { + return null; + } + + // Getting an authentication token is not supported + @Override + public Bundle getAuthToken( + AccountAuthenticatorResponse r, + Account account, + String s, + Bundle bundle) throws NetworkErrorException { + throw new UnsupportedOperationException(); + } + + // Getting a label for the auth token is not supported + @Override + public String getAuthTokenLabel(String s) { + throw new UnsupportedOperationException(); + } + + // Updating user credentials is not supported + @Override + public Bundle updateCredentials( + AccountAuthenticatorResponse r, + Account account, + String s, Bundle bundle) throws NetworkErrorException { + throw new UnsupportedOperationException(); + } + + // Checking features for the account is not supported + @Override + public Bundle hasFeatures( + AccountAuthenticatorResponse r, + Account account, String[] strings) throws NetworkErrorException { + throw new UnsupportedOperationException(); + } +} diff --git a/app/src/main/java/com/example/android/sunshine/app/sync/SunshineAuthenticatorService.java b/app/src/main/java/com/example/android/sunshine/app/sync/SunshineAuthenticatorService.java new file mode 100644 index 000000000..1891e6ce4 --- /dev/null +++ b/app/src/main/java/com/example/android/sunshine/app/sync/SunshineAuthenticatorService.java @@ -0,0 +1,28 @@ +package com.example.android.sunshine.app.sync; + +import android.app.Service; +import android.content.Intent; +import android.os.IBinder; + +/** + * The service which allows the sync adapter framework to access the authenticator. + */ +public class SunshineAuthenticatorService extends Service { + // Instance field that stores the authenticator object + private SunshineAuthenticator mAuthenticator; + + @Override + public void onCreate() { + // Create a new authenticator object + mAuthenticator = new SunshineAuthenticator(this); + } + + /* + * When the system binds to this Service to make the RPC call + * return the authenticator's IBinder. + */ + @Override + public IBinder onBind(Intent intent) { + return mAuthenticator.getIBinder(); + } +} diff --git a/app/src/main/java/com/example/android/sunshine/app/sync/SunshineSyncAdapter.java b/app/src/main/java/com/example/android/sunshine/app/sync/SunshineSyncAdapter.java new file mode 100644 index 000000000..b43b29e68 --- /dev/null +++ b/app/src/main/java/com/example/android/sunshine/app/sync/SunshineSyncAdapter.java @@ -0,0 +1,539 @@ +package com.example.android.sunshine.app.sync; + +import android.accounts.Account; +import android.accounts.AccountManager; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.AbstractThreadedSyncAdapter; +import android.content.ContentProviderClient; +import android.content.ContentResolver; +import android.content.ContentUris; +import android.content.ContentValues; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.content.SyncRequest; +import android.content.SyncResult; +import android.content.res.Resources; +import android.database.Cursor; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.net.Uri; +import android.os.Build; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.support.v4.app.NotificationCompat; +import android.support.v4.app.TaskStackBuilder; +import android.text.format.Time; +import android.util.Log; + +import com.example.android.sunshine.app.BuildConfig; +import com.example.android.sunshine.app.MainActivity; +import com.example.android.sunshine.app.R; +import com.example.android.sunshine.app.Utility; +import com.example.android.sunshine.app.data.WeatherContract; + +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.URL; +import java.util.Vector; + +public class SunshineSyncAdapter extends AbstractThreadedSyncAdapter { + public final String LOG_TAG = SunshineSyncAdapter.class.getSimpleName(); + // Interval at which to sync with the weather, in seconds. + // 60 seconds (1 minute) * 180 = 3 hours + public static final int SYNC_INTERVAL = 60 * 180; + public static final int SYNC_FLEXTIME = SYNC_INTERVAL/3; + private static final long DAY_IN_MILLIS = 1000 * 60 * 60 * 24; + private static final int WEATHER_NOTIFICATION_ID = 3004; + + + private static final String[] NOTIFY_WEATHER_PROJECTION = new String[] { + WeatherContract.WeatherEntry.COLUMN_WEATHER_ID, + WeatherContract.WeatherEntry.COLUMN_MAX_TEMP, + WeatherContract.WeatherEntry.COLUMN_MIN_TEMP, + WeatherContract.WeatherEntry.COLUMN_SHORT_DESC + }; + + // these indices must match the projection + private static final int INDEX_WEATHER_ID = 0; + private static final int INDEX_MAX_TEMP = 1; + private static final int INDEX_MIN_TEMP = 2; + private static final int INDEX_SHORT_DESC = 3; + + public SunshineSyncAdapter(Context context, boolean autoInitialize) { + super(context, autoInitialize); + } + + @Override + public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) { + Log.d(LOG_TAG, "Starting sync"); + String locationQuery = Utility.getPreferredLocation(getContext()); + + // These two need to be declared outside the try/catch + // so that they can be closed in the finally block. + HttpURLConnection urlConnection = null; + BufferedReader reader = null; + + // Will contain the raw JSON response as a string. + String forecastJsonStr = null; + + String format = "json"; + String units = "metric"; + int numDays = 14; + + try { + // Construct the URL for the OpenWeatherMap query + // Possible parameters are avaiable at OWM's forecast API page, at + // http://openweathermap.org/API#forecast + final String FORECAST_BASE_URL = + "http://api.openweathermap.org/data/2.5/forecast/daily?"; + final String QUERY_PARAM = "q"; + final String FORMAT_PARAM = "mode"; + final String UNITS_PARAM = "units"; + final String DAYS_PARAM = "cnt"; + final String APPID_PARAM = "APPID"; + + Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon() + .appendQueryParameter(QUERY_PARAM, locationQuery) + .appendQueryParameter(FORMAT_PARAM, format) + .appendQueryParameter(UNITS_PARAM, units) + .appendQueryParameter(DAYS_PARAM, Integer.toString(numDays)) + .appendQueryParameter(APPID_PARAM, BuildConfig.OPEN_WEATHER_MAP_API_KEY) + .build(); + + URL url = new URL(builtUri.toString()); + + // Create the request to OpenWeatherMap, and open the connection + urlConnection = (HttpURLConnection) url.openConnection(); + urlConnection.setRequestMethod("GET"); + urlConnection.connect(); + + // Read the input stream into a String + InputStream inputStream = urlConnection.getInputStream(); + StringBuffer buffer = new StringBuffer(); + if (inputStream == null) { + // Nothing to do. + return; + } + reader = new BufferedReader(new InputStreamReader(inputStream)); + + String line; + while ((line = reader.readLine()) != null) { + // Since it's JSON, adding a newline isn't necessary (it won't affect parsing) + // But it does make debugging a *lot* easier if you print out the completed + // buffer for debugging. + buffer.append(line + "\n"); + } + + if (buffer.length() == 0) { + // Stream was empty. No point in parsing. + return; + } + forecastJsonStr = buffer.toString(); + getWeatherDataFromJson(forecastJsonStr, locationQuery); + } catch (IOException e) { + Log.e(LOG_TAG, "Error ", e); + // If the code didn't successfully get the weather data, there's no point in attempting + // to parse it. + } catch (JSONException e) { + Log.e(LOG_TAG, e.getMessage(), e); + e.printStackTrace(); + } finally { + if (urlConnection != null) { + urlConnection.disconnect(); + } + if (reader != null) { + try { + reader.close(); + } catch (final IOException e) { + Log.e(LOG_TAG, "Error closing stream", e); + } + } + } + return; + } + + /** + * Take the String representing the complete forecast in JSON Format and + * pull out the data we need to construct the Strings needed for the wireframes. + * + * 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) + throws JSONException { + + // Now we have a String representing the complete forecast in JSON Format. + // Fortunately parsing is easy: constructor takes the JSON string and converts it + // into an Object hierarchy for us. + + // These are the names of the JSON objects that need to be extracted. + + // Location information + final String OWM_CITY = "city"; + final String OWM_CITY_NAME = "name"; + final String OWM_COORD = "coord"; + + // Location coordinate + final String OWM_LATITUDE = "lat"; + final String OWM_LONGITUDE = "lon"; + + // Weather information. Each day's forecast info is an element of the "list" array. + final String OWM_LIST = "list"; + + final String OWM_PRESSURE = "pressure"; + final String OWM_HUMIDITY = "humidity"; + final String OWM_WINDSPEED = "speed"; + final String OWM_WIND_DIRECTION = "deg"; + + // All temperatures are children of the "temp" object. + final String OWM_TEMPERATURE = "temp"; + final String OWM_MAX = "max"; + final String OWM_MIN = "min"; + + final String OWM_WEATHER = "weather"; + final String OWM_DESCRIPTION = "main"; + final String OWM_WEATHER_ID = "id"; + + try { + JSONObject forecastJson = new JSONObject(forecastJsonStr); + JSONArray weatherArray = forecastJson.getJSONArray(OWM_LIST); + + JSONObject cityJson = forecastJson.getJSONObject(OWM_CITY); + String cityName = cityJson.getString(OWM_CITY_NAME); + + JSONObject cityCoord = cityJson.getJSONObject(OWM_COORD); + double cityLatitude = cityCoord.getDouble(OWM_LATITUDE); + double cityLongitude = cityCoord.getDouble(OWM_LONGITUDE); + + long locationId = addLocation(locationSetting, cityName, cityLatitude, cityLongitude); + + // Insert the new weather information into the database + Vector cVVector = new Vector(weatherArray.length()); + + // OWM returns daily forecasts based upon the local time of the city that is being + // asked for, which means that we need to know the GMT offset to translate this data + // properly. + + // Since this data is also sent in-order and the first day is always the + // current day, we're going to take advantage of that to get a nice + // normalized UTC date for all of our weather. + + Time dayTime = new Time(); + dayTime.setToNow(); + + // we start at the day returned by local time. Otherwise this is a mess. + int julianStartDay = Time.getJulianDay(System.currentTimeMillis(), dayTime.gmtoff); + + // now we work exclusively in UTC + dayTime = new Time(); + + for(int i = 0; i < weatherArray.length(); i++) { + // These are the values that will be collected. + long dateTime; + double pressure; + int humidity; + double windSpeed; + double windDirection; + + double high; + double low; + + String description; + int weatherId; + + // Get the JSON object representing the day + JSONObject dayForecast = weatherArray.getJSONObject(i); + + // Cheating to convert this to UTC time, which is what we want anyhow + dateTime = dayTime.setJulianDay(julianStartDay+i); + + pressure = dayForecast.getDouble(OWM_PRESSURE); + humidity = dayForecast.getInt(OWM_HUMIDITY); + windSpeed = dayForecast.getDouble(OWM_WINDSPEED); + windDirection = dayForecast.getDouble(OWM_WIND_DIRECTION); + + // Description is in a child array called "weather", which is 1 element long. + // That element also contains a weather code. + JSONObject weatherObject = + dayForecast.getJSONArray(OWM_WEATHER).getJSONObject(0); + description = weatherObject.getString(OWM_DESCRIPTION); + weatherId = weatherObject.getInt(OWM_WEATHER_ID); + + // Temperatures are in a child object called "temp". Try not to name variables + // "temp" when working with temperature. It confuses everybody. + JSONObject temperatureObject = dayForecast.getJSONObject(OWM_TEMPERATURE); + high = temperatureObject.getDouble(OWM_MAX); + low = temperatureObject.getDouble(OWM_MIN); + + ContentValues weatherValues = new ContentValues(); + + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_LOC_KEY, locationId); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_DATE, dateTime); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_HUMIDITY, humidity); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_PRESSURE, pressure); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_WIND_SPEED, windSpeed); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_DEGREES, windDirection); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_MAX_TEMP, high); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_MIN_TEMP, low); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_SHORT_DESC, description); + weatherValues.put(WeatherContract.WeatherEntry.COLUMN_WEATHER_ID, weatherId); + + cVVector.add(weatherValues); + } + + int inserted = 0; + // add to database + if ( cVVector.size() > 0 ) { + ContentValues[] cvArray = new ContentValues[cVVector.size()]; + cVVector.toArray(cvArray); + getContext().getContentResolver().bulkInsert(WeatherContract.WeatherEntry.CONTENT_URI, cvArray); + + // delete old data so we don't build up an endless history + getContext().getContentResolver().delete(WeatherContract.WeatherEntry.CONTENT_URI, + WeatherContract.WeatherEntry.COLUMN_DATE + " <= ?", + new String[] {Long.toString(dayTime.setJulianDay(julianStartDay-1))}); + + notifyWeather(); + } + + Log.d(LOG_TAG, "Sync Complete. " + cVVector.size() + " Inserted"); + + } catch (JSONException e) { + Log.e(LOG_TAG, e.getMessage(), e); + e.printStackTrace(); + } + } + + private void notifyWeather() { + Context context = getContext(); + //checking the last update and notify if it' the first of the day + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + String displayNotificationsKey = context.getString(R.string.pref_enable_notifications_key); + boolean displayNotifications = prefs.getBoolean(displayNotificationsKey, + Boolean.parseBoolean(context.getString(R.string.pref_enable_notifications_default))); + + if ( displayNotifications ) { + + String lastNotificationKey = context.getString(R.string.pref_last_notification); + long lastSync = prefs.getLong(lastNotificationKey, 0); + + if (System.currentTimeMillis() - lastSync >= DAY_IN_MILLIS) { + // Last sync was more than 1 day ago, let's send a notification with the weather. + String locationQuery = Utility.getPreferredLocation(context); + + Uri weatherUri = WeatherContract.WeatherEntry.buildWeatherLocationWithDate(locationQuery, System.currentTimeMillis()); + + // we'll query our contentProvider, as always + Cursor cursor = context.getContentResolver().query(weatherUri, NOTIFY_WEATHER_PROJECTION, null, null, null); + + if (cursor.moveToFirst()) { + int weatherId = cursor.getInt(INDEX_WEATHER_ID); + double high = cursor.getDouble(INDEX_MAX_TEMP); + double low = cursor.getDouble(INDEX_MIN_TEMP); + String desc = cursor.getString(INDEX_SHORT_DESC); + + int iconId = Utility.getIconResourceForWeatherCondition(weatherId); + Resources resources = context.getResources(); + Bitmap largeIcon = BitmapFactory.decodeResource(resources, + Utility.getArtResourceForWeatherCondition(weatherId)); + String title = context.getString(R.string.app_name); + + // Define the text of the forecast. + String contentText = String.format(context.getString(R.string.format_notification), + desc, + Utility.formatTemperature(context, high), + Utility.formatTemperature(context, low)); + + // NotificationCompatBuilder is a very convenient way to build backward-compatible + // notifications. Just throw in some data. + NotificationCompat.Builder mBuilder = + new NotificationCompat.Builder(getContext()) + .setColor(resources.getColor(R.color.sunshine_light_blue)) + .setSmallIcon(iconId) + .setLargeIcon(largeIcon) + .setContentTitle(title) + .setContentText(contentText); + + // Make something interesting happen when the user clicks on the notification. + // In this case, opening the app is sufficient. + Intent resultIntent = new Intent(context, MainActivity.class); + + // The stack builder object will contain an artificial back stack for the + // started Activity. + // This ensures that navigating backward from the Activity leads out of + // your application to the Home screen. + TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); + stackBuilder.addNextIntent(resultIntent); + PendingIntent resultPendingIntent = + stackBuilder.getPendingIntent( + 0, + PendingIntent.FLAG_UPDATE_CURRENT + ); + mBuilder.setContentIntent(resultPendingIntent); + + NotificationManager mNotificationManager = + (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE); + // WEATHER_NOTIFICATION_ID allows you to update the notification later on. + mNotificationManager.notify(WEATHER_NOTIFICATION_ID, mBuilder.build()); + + //refreshing last sync + SharedPreferences.Editor editor = prefs.edit(); + editor.putLong(lastNotificationKey, System.currentTimeMillis()); + editor.commit(); + } + cursor.close(); + } + } + } + + /** + * Helper method to handle insertion of a new location in the weather database. + * + * @param locationSetting The location string used to request updates from the server. + * @param cityName A human-readable city name, e.g "Mountain View" + * @param lat the latitude of the city + * @param lon the longitude of the city + * @return the row ID of the added location. + */ + long addLocation(String locationSetting, String cityName, double lat, double lon) { + long locationId; + + // First, check if the location with this city name exists in the db + Cursor locationCursor = getContext().getContentResolver().query( + WeatherContract.LocationEntry.CONTENT_URI, + new String[]{WeatherContract.LocationEntry._ID}, + WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?", + new String[]{locationSetting}, + null); + + if (locationCursor.moveToFirst()) { + int locationIdIndex = locationCursor.getColumnIndex(WeatherContract.LocationEntry._ID); + locationId = locationCursor.getLong(locationIdIndex); + } else { + // Now that the content provider is set up, inserting rows of data is pretty simple. + // First create a ContentValues object to hold the data you want to insert. + ContentValues locationValues = new ContentValues(); + + // Then add the data, along with the corresponding name of the data type, + // so the content provider knows what kind of value is being inserted. + locationValues.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName); + locationValues.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting); + locationValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat); + locationValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon); + + // Finally, insert location data into the database. + Uri insertedUri = getContext().getContentResolver().insert( + WeatherContract.LocationEntry.CONTENT_URI, + locationValues + ); + + // The resulting URI contains the ID for the row. Extract the locationId from the Uri. + locationId = ContentUris.parseId(insertedUri); + } + + locationCursor.close(); + // Wait, that worked? Yes! + return locationId; + } + + /** + * Helper method to schedule the sync adapter periodic execution + */ + public static void configurePeriodicSync(Context context, int syncInterval, int flexTime) { + Account account = getSyncAccount(context); + String authority = context.getString(R.string.content_authority); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + // we can enable inexact timers in our periodic sync + SyncRequest request = new SyncRequest.Builder(). + syncPeriodic(syncInterval, flexTime). + setSyncAdapter(account, authority). + setExtras(new Bundle()).build(); + ContentResolver.requestSync(request); + } else { + ContentResolver.addPeriodicSync(account, + authority, new Bundle(), syncInterval); + } + } + + /** + * Helper method to have the sync adapter sync immediately + * @param context The context used to access the account service + */ + public static void syncImmediately(Context context) { + Bundle bundle = new Bundle(); + bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true); + bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); + ContentResolver.requestSync(getSyncAccount(context), + context.getString(R.string.content_authority), bundle); + } + + /** + * Helper method to get the fake account to be used with SyncAdapter, or make a new one + * if the fake account doesn't exist yet. If we make a new account, we call the + * onAccountCreated method so we can initialize things. + * + * @param context The context used to access the account service + * @return a fake account. + */ + public static Account getSyncAccount(Context context) { + // Get an instance of the Android account manager + AccountManager accountManager = + (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE); + + // Create the account type and default account + Account newAccount = new Account( + context.getString(R.string.app_name), context.getString(R.string.sync_account_type)); + + // If the password doesn't exist, the account doesn't exist + if ( null == accountManager.getPassword(newAccount) ) { + + /* + * Add the account and account type, no password or user data + * If successful, return the Account object, otherwise report an error. + */ + if (!accountManager.addAccountExplicitly(newAccount, "", null)) { + return null; + } + /* + * If you don't set android:syncable="true" in + * in your element in the manifest, + * then call ContentResolver.setIsSyncable(account, AUTHORITY, 1) + * here. + */ + + onAccountCreated(newAccount, context); + } + return newAccount; + } + + private static void onAccountCreated(Account newAccount, Context context) { + /* + * Since we've created an account + */ + SunshineSyncAdapter.configurePeriodicSync(context, SYNC_INTERVAL, SYNC_FLEXTIME); + + /* + * Without calling setSyncAutomatically, our periodic sync will not be enabled. + */ + ContentResolver.setSyncAutomatically(newAccount, context.getString(R.string.content_authority), true); + + /* + * Finally, let's do a sync to get things started + */ + syncImmediately(context); + } + + public static void initializeSyncAdapter(Context context) { + getSyncAccount(context); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/android/sunshine/app/sync/SunshineSyncService.java b/app/src/main/java/com/example/android/sunshine/app/sync/SunshineSyncService.java new file mode 100644 index 000000000..8318d5855 --- /dev/null +++ b/app/src/main/java/com/example/android/sunshine/app/sync/SunshineSyncService.java @@ -0,0 +1,26 @@ +package com.example.android.sunshine.app.sync; + +import android.app.Service; +import android.content.Intent; +import android.os.IBinder; +import android.util.Log; + +public class SunshineSyncService extends Service { + private static final Object sSyncAdapterLock = new Object(); + private static SunshineSyncAdapter sSunshineSyncAdapter = null; + + @Override + public void onCreate() { + Log.d("SunshineSyncService", "onCreate - SunshineSyncService"); + synchronized (sSyncAdapterLock) { + if (sSunshineSyncAdapter == null) { + sSunshineSyncAdapter = new SunshineSyncAdapter(getApplicationContext(), true); + } + } + } + + @Override + public IBinder onBind(Intent intent) { + return sSunshineSyncAdapter.getSyncAdapterBinder(); + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable-hdpi/art_clear.png b/app/src/main/res/drawable-hdpi/art_clear.png new file mode 100755 index 000000000..6454f6615 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/art_clear.png differ diff --git a/app/src/main/res/drawable-hdpi/art_clouds.png b/app/src/main/res/drawable-hdpi/art_clouds.png new file mode 100755 index 000000000..14d4f7da8 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/art_clouds.png differ diff --git a/app/src/main/res/drawable-hdpi/art_fog.png b/app/src/main/res/drawable-hdpi/art_fog.png new file mode 100755 index 000000000..81a122130 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/art_fog.png differ diff --git a/app/src/main/res/drawable-hdpi/art_light_clouds.png b/app/src/main/res/drawable-hdpi/art_light_clouds.png new file mode 100755 index 000000000..f51c1bdb7 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/art_light_clouds.png differ diff --git a/app/src/main/res/drawable-hdpi/art_light_rain.png b/app/src/main/res/drawable-hdpi/art_light_rain.png new file mode 100755 index 000000000..019507517 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/art_light_rain.png differ diff --git a/app/src/main/res/drawable-hdpi/art_rain.png b/app/src/main/res/drawable-hdpi/art_rain.png new file mode 100755 index 000000000..1979544a3 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/art_rain.png differ diff --git a/app/src/main/res/drawable-hdpi/art_snow.png b/app/src/main/res/drawable-hdpi/art_snow.png new file mode 100755 index 000000000..512fcdd44 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/art_snow.png differ diff --git a/app/src/main/res/drawable-hdpi/art_storm.png b/app/src/main/res/drawable-hdpi/art_storm.png new file mode 100755 index 000000000..ec8cc9732 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/art_storm.png differ diff --git a/app/src/main/res/drawable-hdpi/ic_clear.png b/app/src/main/res/drawable-hdpi/ic_clear.png new file mode 100755 index 000000000..3313c3abf Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_clear.png differ diff --git a/app/src/main/res/drawable-hdpi/ic_cloudy.png b/app/src/main/res/drawable-hdpi/ic_cloudy.png new file mode 100755 index 000000000..a5f1a5a50 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_cloudy.png differ diff --git a/app/src/main/res/drawable-hdpi/ic_fog.png b/app/src/main/res/drawable-hdpi/ic_fog.png new file mode 100755 index 000000000..990c8e342 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_fog.png differ diff --git a/app/src/main/res/drawable-hdpi/ic_launcher.png b/app/src/main/res/drawable-hdpi/ic_launcher.png deleted file mode 100644 index 786258ade..000000000 Binary files a/app/src/main/res/drawable-hdpi/ic_launcher.png and /dev/null differ diff --git a/app/src/main/res/drawable-hdpi/ic_light_clouds.png b/app/src/main/res/drawable-hdpi/ic_light_clouds.png new file mode 100755 index 000000000..fe38e489c Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_light_clouds.png differ diff --git a/app/src/main/res/drawable-hdpi/ic_light_rain.png b/app/src/main/res/drawable-hdpi/ic_light_rain.png new file mode 100755 index 000000000..ae9468c38 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_light_rain.png differ diff --git a/app/src/main/res/drawable-hdpi/ic_logo.png b/app/src/main/res/drawable-hdpi/ic_logo.png new file mode 100755 index 000000000..228963796 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_logo.png differ diff --git a/app/src/main/res/drawable-hdpi/ic_rain.png b/app/src/main/res/drawable-hdpi/ic_rain.png new file mode 100755 index 000000000..0e858264a Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_rain.png differ diff --git a/app/src/main/res/drawable-hdpi/ic_snow.png b/app/src/main/res/drawable-hdpi/ic_snow.png new file mode 100755 index 000000000..0f8bfab99 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_snow.png differ diff --git a/app/src/main/res/drawable-hdpi/ic_storm.png b/app/src/main/res/drawable-hdpi/ic_storm.png new file mode 100755 index 000000000..27e5429b0 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_storm.png differ diff --git a/app/src/main/res/drawable-mdpi/art_clear.png b/app/src/main/res/drawable-mdpi/art_clear.png new file mode 100755 index 000000000..2cf330ac9 Binary files /dev/null and b/app/src/main/res/drawable-mdpi/art_clear.png differ diff --git a/app/src/main/res/drawable-mdpi/art_clouds.png b/app/src/main/res/drawable-mdpi/art_clouds.png new file mode 100755 index 000000000..5aa10ca37 Binary files /dev/null and b/app/src/main/res/drawable-mdpi/art_clouds.png differ diff --git a/app/src/main/res/drawable-mdpi/art_fog.png b/app/src/main/res/drawable-mdpi/art_fog.png new file mode 100755 index 000000000..1357a2477 Binary files /dev/null and b/app/src/main/res/drawable-mdpi/art_fog.png differ diff --git a/app/src/main/res/drawable-mdpi/art_light_clouds.png b/app/src/main/res/drawable-mdpi/art_light_clouds.png new file mode 100755 index 000000000..7eecc6a73 Binary files /dev/null and b/app/src/main/res/drawable-mdpi/art_light_clouds.png differ diff --git a/app/src/main/res/drawable-mdpi/art_light_rain.png b/app/src/main/res/drawable-mdpi/art_light_rain.png new file mode 100755 index 000000000..8e6016546 Binary files /dev/null and b/app/src/main/res/drawable-mdpi/art_light_rain.png differ diff --git a/app/src/main/res/drawable-mdpi/art_rain.png b/app/src/main/res/drawable-mdpi/art_rain.png new file mode 100755 index 000000000..3a518e59f Binary files /dev/null and b/app/src/main/res/drawable-mdpi/art_rain.png differ diff --git a/app/src/main/res/drawable-mdpi/art_snow.png b/app/src/main/res/drawable-mdpi/art_snow.png new file mode 100755 index 000000000..7491c5ef9 Binary files /dev/null and b/app/src/main/res/drawable-mdpi/art_snow.png differ diff --git a/app/src/main/res/drawable-mdpi/art_storm.png b/app/src/main/res/drawable-mdpi/art_storm.png new file mode 100755 index 000000000..9aee2f324 Binary files /dev/null and b/app/src/main/res/drawable-mdpi/art_storm.png differ diff --git a/app/src/main/res/drawable-mdpi/ic_clear.png b/app/src/main/res/drawable-mdpi/ic_clear.png new file mode 100755 index 000000000..b6a5426c8 Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_clear.png differ diff --git a/app/src/main/res/drawable-mdpi/ic_cloudy.png b/app/src/main/res/drawable-mdpi/ic_cloudy.png new file mode 100755 index 000000000..ef6f0253b Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_cloudy.png differ diff --git a/app/src/main/res/drawable-mdpi/ic_fog.png b/app/src/main/res/drawable-mdpi/ic_fog.png new file mode 100755 index 000000000..95383c160 Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_fog.png differ diff --git a/app/src/main/res/drawable-mdpi/ic_launcher.png b/app/src/main/res/drawable-mdpi/ic_launcher.png deleted file mode 100644 index f415afeb3..000000000 Binary files a/app/src/main/res/drawable-mdpi/ic_launcher.png and /dev/null differ diff --git a/app/src/main/res/drawable-mdpi/ic_light_clouds.png b/app/src/main/res/drawable-mdpi/ic_light_clouds.png new file mode 100755 index 000000000..1aaf92565 Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_light_clouds.png differ diff --git a/app/src/main/res/drawable-mdpi/ic_light_rain.png b/app/src/main/res/drawable-mdpi/ic_light_rain.png new file mode 100755 index 000000000..2b7133a6a Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_light_rain.png differ diff --git a/app/src/main/res/drawable-mdpi/ic_logo.png b/app/src/main/res/drawable-mdpi/ic_logo.png new file mode 100755 index 000000000..464a1022d Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_logo.png differ diff --git a/app/src/main/res/drawable-mdpi/ic_rain.png b/app/src/main/res/drawable-mdpi/ic_rain.png new file mode 100755 index 000000000..f70705604 Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_rain.png differ diff --git a/app/src/main/res/drawable-mdpi/ic_snow.png b/app/src/main/res/drawable-mdpi/ic_snow.png new file mode 100755 index 000000000..2970d9b7e Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_snow.png differ diff --git a/app/src/main/res/drawable-mdpi/ic_storm.png b/app/src/main/res/drawable-mdpi/ic_storm.png new file mode 100755 index 000000000..40649b2ef Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_storm.png differ diff --git a/app/src/main/res/drawable-v21/today_touch_selector.xml b/app/src/main/res/drawable-v21/today_touch_selector.xml new file mode 100644 index 000000000..1d3d708b4 --- /dev/null +++ b/app/src/main/res/drawable-v21/today_touch_selector.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable-v21/touch_selector.xml b/app/src/main/res/drawable-v21/touch_selector.xml new file mode 100644 index 000000000..22b97f558 --- /dev/null +++ b/app/src/main/res/drawable-v21/touch_selector.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable-xhdpi/art_clear.png b/app/src/main/res/drawable-xhdpi/art_clear.png new file mode 100755 index 000000000..bfa8854e5 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/art_clear.png differ diff --git a/app/src/main/res/drawable-xhdpi/art_clouds.png b/app/src/main/res/drawable-xhdpi/art_clouds.png new file mode 100755 index 000000000..d2d8a48ef Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/art_clouds.png differ diff --git a/app/src/main/res/drawable-xhdpi/art_fog.png b/app/src/main/res/drawable-xhdpi/art_fog.png new file mode 100755 index 000000000..de312d31a Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/art_fog.png differ diff --git a/app/src/main/res/drawable-xhdpi/art_light_clouds.png b/app/src/main/res/drawable-xhdpi/art_light_clouds.png new file mode 100755 index 000000000..6a3f64a6d Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/art_light_clouds.png differ diff --git a/app/src/main/res/drawable-xhdpi/art_light_rain.png b/app/src/main/res/drawable-xhdpi/art_light_rain.png new file mode 100755 index 000000000..10fc0bd2a Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/art_light_rain.png differ diff --git a/app/src/main/res/drawable-xhdpi/art_rain.png b/app/src/main/res/drawable-xhdpi/art_rain.png new file mode 100755 index 000000000..03555f650 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/art_rain.png differ diff --git a/app/src/main/res/drawable-xhdpi/art_snow.png b/app/src/main/res/drawable-xhdpi/art_snow.png new file mode 100755 index 000000000..9b41604ec Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/art_snow.png differ diff --git a/app/src/main/res/drawable-xhdpi/art_storm.png b/app/src/main/res/drawable-xhdpi/art_storm.png new file mode 100755 index 000000000..72165118a Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/art_storm.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_clear.png b/app/src/main/res/drawable-xhdpi/ic_clear.png new file mode 100755 index 000000000..e6252d587 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_clear.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_cloudy.png b/app/src/main/res/drawable-xhdpi/ic_cloudy.png new file mode 100755 index 000000000..4b5cd7c32 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_cloudy.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_fog.png b/app/src/main/res/drawable-xhdpi/ic_fog.png new file mode 100755 index 000000000..33c152a21 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_fog.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_launcher.png b/app/src/main/res/drawable-xhdpi/ic_launcher.png deleted file mode 100644 index 5fba33f65..000000000 Binary files a/app/src/main/res/drawable-xhdpi/ic_launcher.png and /dev/null differ diff --git a/app/src/main/res/drawable-xhdpi/ic_light_clouds.png b/app/src/main/res/drawable-xhdpi/ic_light_clouds.png new file mode 100755 index 000000000..712cc7392 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_light_clouds.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_light_rain.png b/app/src/main/res/drawable-xhdpi/ic_light_rain.png new file mode 100755 index 000000000..5521b1b42 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_light_rain.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_logo.png b/app/src/main/res/drawable-xhdpi/ic_logo.png new file mode 100755 index 000000000..02fc44ac1 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_logo.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_rain.png b/app/src/main/res/drawable-xhdpi/ic_rain.png new file mode 100755 index 000000000..f3acb4d1d Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_rain.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_snow.png b/app/src/main/res/drawable-xhdpi/ic_snow.png new file mode 100755 index 000000000..791967b31 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_snow.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_storm.png b/app/src/main/res/drawable-xhdpi/ic_storm.png new file mode 100755 index 000000000..3ddfade8f Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_storm.png differ diff --git a/app/src/main/res/drawable-xxhdpi/art_clear.png b/app/src/main/res/drawable-xxhdpi/art_clear.png new file mode 100755 index 000000000..55dc436ea Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/art_clear.png differ diff --git a/app/src/main/res/drawable-xxhdpi/art_clouds.png b/app/src/main/res/drawable-xxhdpi/art_clouds.png new file mode 100755 index 000000000..13fe27229 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/art_clouds.png differ diff --git a/app/src/main/res/drawable-xxhdpi/art_fog.png b/app/src/main/res/drawable-xxhdpi/art_fog.png new file mode 100755 index 000000000..8e7574bc1 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/art_fog.png differ diff --git a/app/src/main/res/drawable-xxhdpi/art_light_clouds.png b/app/src/main/res/drawable-xxhdpi/art_light_clouds.png new file mode 100755 index 000000000..8a33e1be8 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/art_light_clouds.png differ diff --git a/app/src/main/res/drawable-xxhdpi/art_light_rain.png b/app/src/main/res/drawable-xxhdpi/art_light_rain.png new file mode 100755 index 000000000..84437180c Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/art_light_rain.png differ diff --git a/app/src/main/res/drawable-xxhdpi/art_rain.png b/app/src/main/res/drawable-xxhdpi/art_rain.png new file mode 100755 index 000000000..921bb1463 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/art_rain.png differ diff --git a/app/src/main/res/drawable-xxhdpi/art_snow.png b/app/src/main/res/drawable-xxhdpi/art_snow.png new file mode 100755 index 000000000..b5892cead Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/art_snow.png differ diff --git a/app/src/main/res/drawable-xxhdpi/art_storm.png b/app/src/main/res/drawable-xxhdpi/art_storm.png new file mode 100755 index 000000000..5f73b32ad Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/art_storm.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_clear.png b/app/src/main/res/drawable-xxhdpi/ic_clear.png new file mode 100755 index 000000000..221d12413 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_clear.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_cloudy.png b/app/src/main/res/drawable-xxhdpi/ic_cloudy.png new file mode 100755 index 000000000..c8c08b89e Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_cloudy.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_fog.png b/app/src/main/res/drawable-xxhdpi/ic_fog.png new file mode 100755 index 000000000..38250eb39 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_fog.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_launcher.png b/app/src/main/res/drawable-xxhdpi/ic_launcher.png deleted file mode 100644 index b37fe2bfb..000000000 Binary files a/app/src/main/res/drawable-xxhdpi/ic_launcher.png and /dev/null differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_light_clouds.png b/app/src/main/res/drawable-xxhdpi/ic_light_clouds.png new file mode 100755 index 000000000..97fc9af5c Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_light_clouds.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_light_rain.png b/app/src/main/res/drawable-xxhdpi/ic_light_rain.png new file mode 100755 index 000000000..4da9bb9aa Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_light_rain.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_logo.png b/app/src/main/res/drawable-xxhdpi/ic_logo.png new file mode 100755 index 000000000..9e04aad5a Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_logo.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_rain.png b/app/src/main/res/drawable-xxhdpi/ic_rain.png new file mode 100755 index 000000000..c0d4d5223 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_rain.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_snow.png b/app/src/main/res/drawable-xxhdpi/ic_snow.png new file mode 100755 index 000000000..0ce80853e Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_snow.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_storm.png b/app/src/main/res/drawable-xxhdpi/ic_storm.png new file mode 100755 index 000000000..c1daf9c80 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_storm.png differ diff --git a/app/src/main/res/drawable/today_touch_selector.xml b/app/src/main/res/drawable/today_touch_selector.xml new file mode 100644 index 000000000..b38df2495 --- /dev/null +++ b/app/src/main/res/drawable/today_touch_selector.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/touch_selector.xml b/app/src/main/res/drawable/touch_selector.xml new file mode 100644 index 000000000..7f7aa8b61 --- /dev/null +++ b/app/src/main/res/drawable/touch_selector.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout-sw600dp/activity_main.xml b/app/src/main/res/layout-sw600dp/activity_main.xml new file mode 100644 index 000000000..84e9eec89 --- /dev/null +++ b/app/src/main/res/layout-sw600dp/activity_main.xml @@ -0,0 +1,28 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_detail.xml b/app/src/main/res/layout/activity_detail.xml index 3ecaa61f8..3859e2af7 100644 --- a/app/src/main/res/layout/activity_detail.xml +++ b/app/src/main/res/layout/activity_detail.xml @@ -1,5 +1,7 @@ - + xmlns:tools="http://schemas.android.com/tools" + android:id="@+id/weather_detail_container" + android:layout_width="match_parent" + android:layout_height="match_parent" + tools:context="com.example.android.sunshine.app.DetailActivity" + tools:ignore="MergeRootFrame" /> \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index b69c03abf..94e1e15d7 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,4 +1,8 @@ - + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_detail.xml b/app/src/main/res/layout/fragment_detail.xml index 45e7df4b2..04f8ec7b7 100644 --- a/app/src/main/res/layout/fragment_detail.xml +++ b/app/src/main/res/layout/fragment_detail.xml @@ -1,13 +1,111 @@ - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/fragment_detail_wide.xml b/app/src/main/res/layout/fragment_detail_wide.xml new file mode 100644 index 000000000..2ef7dd504 --- /dev/null +++ b/app/src/main/res/layout/fragment_detail_wide.xml @@ -0,0 +1,107 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_main.xml b/app/src/main/res/layout/fragment_main.xml index 372b60d63..fdde4d3bf 100644 --- a/app/src/main/res/layout/fragment_main.xml +++ b/app/src/main/res/layout/fragment_main.xml @@ -2,16 +2,11 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - android:paddingLeft="@dimen/activity_horizontal_margin" - - android:paddingRight="@dimen/activity_horizontal_margin" - android:paddingTop="@dimen/activity_vertical_margin" - android:paddingBottom="@dimen/activity_vertical_margin" - tools:context=".MainActivity$ForecastFragment"> - + tools:context="com.example.android.sunshine.app.ForecastFragment"> - + android:layout_height="match_parent" + android:divider="@null" /> diff --git a/app/src/main/res/layout/list_item_forecast.xml b/app/src/main/res/layout/list_item_forecast.xml index 965bd5714..e17c45579 100644 --- a/app/src/main/res/layout/list_item_forecast.xml +++ b/app/src/main/res/layout/list_item_forecast.xml @@ -1,8 +1,68 @@ - + \ No newline at end of file + android:minHeight="?android:attr/listPreferredItemHeight" + android:orientation="horizontal" + android:background="@drawable/touch_selector"> + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/list_item_forecast_today.xml b/app/src/main/res/layout/list_item_forecast_today.xml new file mode 100644 index 000000000..01a0ef84e --- /dev/null +++ b/app/src/main/res/layout/list_item_forecast_today.xml @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/detail.xml b/app/src/main/res/menu/detail.xml index 6e04aa16a..865ac0539 100644 --- a/app/src/main/res/menu/detail.xml +++ b/app/src/main/res/menu/detail.xml @@ -1,7 +1,9 @@

- + tools:context="com.example.android.sunshine.app.DetailActivity" > + diff --git a/app/src/main/res/menu/detailfragment.xml b/app/src/main/res/menu/detailfragment.xml new file mode 100644 index 000000000..bfef05f66 --- /dev/null +++ b/app/src/main/res/menu/detailfragment.xml @@ -0,0 +1,9 @@ + + + + + diff --git a/app/src/main/res/menu/forecastfragment.xml b/app/src/main/res/menu/forecastfragment.xml index 50bd1256d..5a3d1b82a 100644 --- a/app/src/main/res/menu/forecastfragment.xml +++ b/app/src/main/res/menu/forecastfragment.xml @@ -2,7 +2,10 @@ - + + + \ No newline at end of file diff --git a/app/src/main/res/menu/main.xml b/app/src/main/res/menu/main.xml index b1cb90811..e418331fe 100644 --- a/app/src/main/res/menu/main.xml +++ b/app/src/main/res/menu/main.xml @@ -1,6 +1,9 @@ - - + xmlns:tools="http://schemas.android.com/tools" + tools:context="com.example.android.sunshine.app.MainActivity" > + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher.png b/app/src/main/res/mipmap-hdpi/ic_launcher.png new file mode 100755 index 000000000..8d3ab05eb Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher.png b/app/src/main/res/mipmap-mdpi/ic_launcher.png new file mode 100755 index 000000000..01f84db4b Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher.png b/app/src/main/res/mipmap-xhdpi/ic_launcher.png new file mode 100755 index 000000000..03c950bc8 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png new file mode 100755 index 000000000..cc53ae908 Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png new file mode 100755 index 000000000..63c32a524 Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/app/src/main/res/values-land/refs.xml b/app/src/main/res/values-land/refs.xml new file mode 100644 index 000000000..fd9597813 --- /dev/null +++ b/app/src/main/res/values-land/refs.xml @@ -0,0 +1,8 @@ + + + + @layout/fragment_detail_wide + \ No newline at end of file diff --git a/app/src/main/res/values-sw600dp/refs.xml b/app/src/main/res/values-sw600dp/refs.xml new file mode 100644 index 000000000..84c4f7c1a --- /dev/null +++ b/app/src/main/res/values-sw600dp/refs.xml @@ -0,0 +1,9 @@ + + + + + @layout/fragment_detail_wide + diff --git a/app/src/main/res/values-sw600dp/styles.xml b/app/src/main/res/values-sw600dp/styles.xml new file mode 100644 index 000000000..a994279c2 --- /dev/null +++ b/app/src/main/res/values-sw600dp/styles.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-v14/styles.xml b/app/src/main/res/values-v14/styles.xml new file mode 100644 index 000000000..a3a9ec45f --- /dev/null +++ b/app/src/main/res/values-v14/styles.xml @@ -0,0 +1,13 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-v21/styles.xml b/app/src/main/res/values-v21/styles.xml new file mode 100644 index 000000000..2e9da223f --- /dev/null +++ b/app/src/main/res/values-v21/styles.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-w820dp/dimens.xml b/app/src/main/res/values-w820dp/dimens.xml deleted file mode 100644 index 63fc81644..000000000 --- a/app/src/main/res/values-w820dp/dimens.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - 64dp - diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml new file mode 100644 index 000000000..f752cb333 --- /dev/null +++ b/app/src/main/res/values/arrays.xml @@ -0,0 +1,13 @@ + + + + + @string/pref_units_label_metric + @string/pref_units_label_imperial + + + + @string/pref_units_metric + @string/pref_units_imperial + + \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml new file mode 100644 index 000000000..0dfb36a37 --- /dev/null +++ b/app/src/main/res/values/colors.xml @@ -0,0 +1,13 @@ + + + + #FFFFFF + #cccccc + #646464 + #000000 + + #ff64c2f4 + #ff1ca8f4 + #0288D1 + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d8d8fd38c..d3d229215 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,5 +1,5 @@ - + Settings + Map Location + Share Refresh Details - Hello world! + Settings - + + Location + + + location + + + 94043 + + + enable_notifications + Weather Notifications + + Enabled + Not Enabled + true + + + + + Temperature Units + + + Metric + + + Imperial + + + units + + + metric + + + imperial + + + + Today + + + Tomorrow + + + %1$s, %2$s + + + + %1.0f\u00B0 + + + + Wind: %1$1.0f mph %2$s + + Wind: %1$1.0f km/h %2$s + + + Pressure: %1.0f hPa + + + Humidity: %1.0f %% + + + sunshine.example.com + com.example.android.sunshine.app + + + Forecast: %1$s High: %2$s Low: %3$s + + + last_notification + + \ No newline at end of file diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 766ab9930..0528bbf76 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -1,8 +1,30 @@ - - - + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/authenticator.xml b/app/src/main/res/xml/authenticator.xml new file mode 100644 index 000000000..e3654bdee --- /dev/null +++ b/app/src/main/res/xml/authenticator.xml @@ -0,0 +1,6 @@ + + \ No newline at end of file diff --git a/app/src/main/res/xml/pref_general.xml b/app/src/main/res/xml/pref_general.xml new file mode 100644 index 000000000..a210c6036 --- /dev/null +++ b/app/src/main/res/xml/pref_general.xml @@ -0,0 +1,27 @@ + + + + + + + + + + diff --git a/app/src/main/res/xml/syncadapter.xml b/app/src/main/res/xml/syncadapter.xml new file mode 100644 index 000000000..a1913e577 --- /dev/null +++ b/app/src/main/res/xml/syncadapter.xml @@ -0,0 +1,9 @@ + + + \ No newline at end of file