Skip to content

Commit 63b76ed

Browse files
LylaAsser
authored andcommitted
Feat: Add updated lesson 10
1 parent 9f138fa commit 63b76ed

File tree

511 files changed

+9577
-2089
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

511 files changed

+9577
-2089
lines changed
Lines changed: 105 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,124 @@
1-
.DS_Store
21

3-
# built application files
2+
# Created by https://www.gitignore.io/api/intellij,android,gradle
3+
4+
### Intellij ###
5+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
6+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
7+
8+
# User-specific stuff:
9+
.idea/workspace.xml
10+
.idea/tasks.xml
11+
.idea/dictionaries
12+
.idea/vcs.xml
13+
.idea/jsLibraryMappings.xml
14+
15+
# Sensitive or high-churn files:
16+
.idea/dataSources.ids
17+
.idea/dataSources.xml
18+
.idea/dataSources.local.xml
19+
.idea/sqlDataSources.xml
20+
.idea/dynamic.xml
21+
.idea/uiDesigner.xml
22+
23+
# Gradle:
24+
.idea/gradle.xml
25+
.idea/libraries
26+
27+
# Mongo Explorer plugin:
28+
.idea/mongoSettings.xml
29+
30+
## File-based project format:
31+
*.iws
32+
33+
## Plugin-specific files:
34+
35+
# IntelliJ
36+
/out/
37+
38+
# mpeltonen/sbt-idea plugin
39+
.idea_modules/
40+
41+
# JIRA plugin
42+
atlassian-ide-plugin.xml
43+
44+
# Crashlytics plugin (for Android Studio and IntelliJ)
45+
com_crashlytics_export_strings.xml
46+
crashlytics.properties
47+
crashlytics-build.properties
48+
fabric.properties
49+
50+
### Intellij Patch ###
51+
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
52+
53+
# *.iml
54+
# modules.xml
55+
# .idea/misc.xml
56+
# *.ipr
57+
58+
59+
### Android ###
60+
# Built application files
461
*.apk
562
*.ap_
663

7-
# files for the dex VM
64+
# Files for the ART/Dalvik VM
865
*.dex
966

1067
# Java class files
1168
*.class
1269

13-
# generated files
70+
# Generated files
1471
bin/
15-
out/
1672
gen/
73+
out/
1774

18-
# Libraries used by the app
19-
# Can explicitly add if we want, but shouldn't do so blindly. Licenses, bloat, etc.
20-
/libs
21-
75+
# Gradle files
76+
.gradle/
77+
build/
2278

23-
# Build stuff (auto-generated by android update project ...)
24-
build.xml
25-
ant.properties
79+
# Local configuration file (sdk path, etc)
2680
local.properties
27-
project.properties
2881

29-
# Eclipse project files
30-
.classpath
31-
.project
82+
# Proguard folder generated by Eclipse
83+
proguard/
84+
85+
# Log Files
86+
*.log
3287

33-
# idea project files
34-
.idea/
88+
# Android Studio Navigation editor temp files
89+
.navigation/
90+
91+
# Android Studio captures folder
92+
captures/
93+
94+
# Intellij
3595
*.iml
36-
*.ipr
37-
*.iws
96+
.idea/workspace.xml
97+
.idea/libraries
98+
99+
# Keystore files
100+
*.jks
38101

39-
# Gradle-based build
102+
### Android Patch ###
103+
gen-external-apklibs
104+
105+
106+
### Gradle ###
40107
.gradle
41-
build/
108+
/build/
109+
110+
# Ignore Gradle GUI config
111+
gradle-app.setting
112+
113+
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
114+
!gradle-wrapper.jar
115+
116+
# Cache of project
117+
.gradletasknamecache
118+
119+
# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898
120+
# gradle/wrapper/gradle-wrapper.properties
121+
122+
#removing .idea folder
123+
124+
.idea/

Lesson10-Hydration-Reminder/T10.01-Exercise-IntentServices/app/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,4 @@ android {
2020
dependencies {
2121
compile fileTree(include: ['*.jar'], dir: 'libs')
2222
compile 'com.android.support:appcompat-v7:25.0.0'
23-
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha9'
2423
}

Lesson10-Hydration-Reminder/T10.01-Exercise-IntentServices/app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
</intent-filter>
1919
</activity>
2020

21-
<!-- TODO (14) Declare WaterReminderIntentService in the Manifest -->
21+
<!-- TODO (14) Declare WaterReminderIntentService in the Manifest and set the exported
22+
attribute to false-->
2223

2324
</application>
2425
</manifest>

Lesson10-Hydration-Reminder/T10.01-Exercise-IntentServices/app/src/main/java/com/example/android/background/MainActivity.java

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020
import android.preference.PreferenceManager;
2121
import android.support.v7.app.AppCompatActivity;
2222
import android.view.View;
23+
import android.widget.ImageView;
2324
import android.widget.TextView;
2425
import android.widget.Toast;
25-
2626
import com.example.android.background.utilities.PreferenceUtilities;
2727

2828
public class MainActivity extends AppCompatActivity implements
2929
SharedPreferences.OnSharedPreferenceChangeListener {
3030

3131
private TextView mWaterCountDisplay;
3232
private TextView mChargingCountDisplay;
33-
private TextView mWifiReminderCountDisplay;
33+
private ImageView mChargingImageView;
3434

3535
private Toast mToast;
3636

@@ -39,73 +39,69 @@ protected void onCreate(Bundle savedInstanceState) {
3939
super.onCreate(savedInstanceState);
4040
setContentView(R.layout.activity_main);
4141

42+
/** Get the views **/
4243
mWaterCountDisplay = (TextView) findViewById(R.id.tv_water_count);
4344
mChargingCountDisplay = (TextView) findViewById(R.id.tv_charging_reminder_count);
44-
mWifiReminderCountDisplay = (TextView) findViewById(R.id.tv_wifi_reminder_count);
45+
mChargingImageView = (ImageView) findViewById(R.id.iv_power_increment);
4546

46-
updateFormattedWaterCount();
47+
/** Set the original values in the UI **/
48+
updateWaterCount();
4749
updateChargingReminderCount();
48-
updateWiFiReminderCount();
4950

51+
/** Setup the shared preference listener **/
5052
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
5153
prefs.registerOnSharedPreferenceChangeListener(this);
5254
}
5355

54-
private void updateFormattedWaterCount() {
56+
/**
57+
* Updates the TextView to display the new water count from SharedPreferences
58+
*/
59+
private void updateWaterCount() {
5560
int waterCount = PreferenceUtilities.getWaterCount(this);
56-
String waterCountDisplayFormat = getString(R.string.water_count_format);
57-
String formattedWaterCount = String.format(
58-
waterCountDisplayFormat,
59-
waterCount);
60-
61-
mWaterCountDisplay.setText(formattedWaterCount);
61+
mWaterCountDisplay.setText(waterCount+"");
6262
}
6363

64+
/**
65+
* Updates the TextView to display the new charging reminder count from SharedPreferences
66+
*/
6467
private void updateChargingReminderCount() {
6568
int chargingReminders = PreferenceUtilities.getChargingReminderCount(this);
66-
String chargingReminderCountFormat = getString(R.string.charging_reminder_count_format);
67-
String formattedChargingReminders = String.format(
68-
chargingReminderCountFormat,
69-
chargingReminders);
70-
69+
String formattedChargingReminders = getResources().getQuantityString(
70+
R.plurals.charge_notification_count, chargingReminders, chargingReminders);
7171
mChargingCountDisplay.setText(formattedChargingReminders);
72-
}
7372

74-
private void updateWiFiReminderCount() {
75-
int wifiReminders = PreferenceUtilities.getWifiReminderCount(this);
76-
String wiFiReminderCountFormat = getString(R.string.wifi_reminder_count_format);
77-
String formattedWifiReminderCount = String.format(
78-
wiFiReminderCountFormat,
79-
wifiReminders);
80-
81-
mWifiReminderCountDisplay.setText(formattedWifiReminderCount);
8273
}
8374

75+
/**
76+
* Adds one to the water count and shows a toast
77+
*/
8478
public void incrementWater(View view) {
8579
if (mToast != null) mToast.cancel();
86-
mToast = Toast.makeText(this, R.string.water_chug, Toast.LENGTH_SHORT);
80+
mToast = Toast.makeText(this, R.string.water_chug_toast, Toast.LENGTH_SHORT);
8781
mToast.show();
88-
89-
// TODO (15) Start WaterReminderIntentService to increment the water count when the button is pressed
82+
// TODO (15) Create an explicit intent for WaterReminderIntentService
83+
// TODO (16) Set the action of the intent to ACTION_INCREMENT_WATER_COUNT
84+
// TODO (17) Call startService and pass the explicit intent you just created
9085
}
9186

9287
@Override
9388
protected void onDestroy() {
9489
super.onDestroy();
95-
90+
/** Cleanup the shared preference listener **/
9691
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
9792
prefs.unregisterOnSharedPreferenceChangeListener(this);
9893
}
9994

95+
/**
96+
* This is a listener that will update the UI when the water count or charging reminder counts
97+
* change
98+
*/
10099
@Override
101100
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
102-
103101
if (PreferenceUtilities.KEY_WATER_COUNT.equals(key)) {
104-
updateFormattedWaterCount();
102+
updateWaterCount();
105103
} else if (PreferenceUtilities.KEY_CHARGING_REMINDER_COUNT.equals(key)) {
106104
updateChargingReminderCount();
107-
} else if (PreferenceUtilities.KEY_WIFI_REMINDER_COUNT.equals(key)) {
108-
updateWiFiReminderCount();
109105
}
110106
}
111107
}

Lesson10-Hydration-Reminder/T10.01-Exercise-IntentServices/app/src/main/java/com/example/android/background/sync/ReminderTasks.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// TODO (6) Create a public static void method called executeTask
88
// TODO (7) Add a Context called context and String parameter called action to the parameter list
99

10-
// TODO (8) If the action equals ACTION_INCREMENT_WATER, call this class's incrementWaterCount
10+
// TODO (8) If the action equals ACTION_INCREMENT_WATER_COUNT, call this class's incrementWaterCount
1111

1212
// TODO (3) Create a private static void method called incrementWaterCount
1313
// TODO (4) Add a Context called context to the argument list

Lesson10-Hydration-Reminder/T10.01-Exercise-IntentServices/app/src/main/java/com/example/android/background/utilities/PreferenceUtilities.java

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@
1919
import android.content.SharedPreferences;
2020
import android.preference.PreferenceManager;
2121

22+
/**
23+
* This class contains utility methods which update water and charging counts in SharedPreferences
24+
*/
2225
public final class PreferenceUtilities {
2326

2427
public static final String KEY_WATER_COUNT = "water-count";
25-
public static final String KEY_WIFI_REMINDER_COUNT = "wifi-reminder-count";
2628
public static final String KEY_CHARGING_REMINDER_COUNT = "charging-reminder-count";
2729

2830
private static final int DEFAULT_COUNT = 0;
2931

30-
synchronized public static void setWaterCount(Context context, int glassesOfWater) {
32+
synchronized private static void setWaterCount(Context context, int glassesOfWater) {
3133
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
3234
SharedPreferences.Editor editor = prefs.edit();
3335
editor.putInt(KEY_WATER_COUNT, glassesOfWater);
@@ -45,21 +47,6 @@ synchronized public static void incrementWaterCount(Context context) {
4547
PreferenceUtilities.setWaterCount(context, ++waterCount);
4648
}
4749

48-
synchronized public static void incrementWifiReminderCount(Context context) {
49-
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
50-
int wifiReminders = prefs.getInt(KEY_WIFI_REMINDER_COUNT, DEFAULT_COUNT);
51-
52-
SharedPreferences.Editor editor = prefs.edit();
53-
editor.putInt(KEY_WIFI_REMINDER_COUNT, ++wifiReminders);
54-
editor.apply();
55-
}
56-
57-
public static int getWifiReminderCount(Context context) {
58-
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
59-
int wifiReminders = prefs.getInt(KEY_WIFI_REMINDER_COUNT, DEFAULT_COUNT);
60-
return wifiReminders;
61-
}
62-
6350
synchronized public static void incrementChargingReminderCount(Context context) {
6451
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
6552
int chargingReminders = prefs.getInt(KEY_CHARGING_REMINDER_COUNT, DEFAULT_COUNT);
484 Bytes
Loading
513 Bytes
Loading
934 Bytes
Loading
343 Bytes
Loading

0 commit comments

Comments
 (0)