diff --git a/SVID_20180321_170127_20180321170943.gif b/SVID_20180321_170127_20180321170943.gif new file mode 100644 index 00000000..3717e0dc Binary files /dev/null and b/SVID_20180321_170127_20180321170943.gif differ diff --git a/SVID_20180323_225239_20180323230757.gif b/SVID_20180323_225239_20180323230757.gif new file mode 100644 index 00000000..6eecf181 Binary files /dev/null and b/SVID_20180323_225239_20180323230757.gif differ diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ac739f17..b897ed26 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,7 +1,7 @@ - + - \ No newline at end of file + diff --git a/app/src/main/java/mg/studio/weatherappdesign/MainActivity.java b/app/src/main/java/mg/studio/weatherappdesign/MainActivity.java index c83b0407..909d8ed1 100644 --- a/app/src/main/java/mg/studio/weatherappdesign/MainActivity.java +++ b/app/src/main/java/mg/studio/weatherappdesign/MainActivity.java @@ -1,10 +1,15 @@ package mg.studio.weatherappdesign; + +import android.net.ConnectivityManager; +import android.net.NetworkInfo; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; +import android.widget.ImageView; import android.widget.TextView; +import android.widget.Toast; import java.io.BufferedReader; import java.io.IOException; @@ -14,10 +19,19 @@ import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; +import java.util.Calendar; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class MainActivity extends AppCompatActivity { - + String[] ADate = new String[]{"MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY","SUNDAY"}; + Pattern p = Pattern.compile("location.{3}([\u4E00-\u9FA5]+).*?cond_txt_d.{3}([\u4E00-\u9FA5]+).*?date.{3}(.{10}).*?tmp_max.{3}(\\d+).*" + + "?cond_txt_d.{3}([\u4E00-\u9FA5]+).*?date.{3}(.{10}).*?tmp_max.{3}(\\d+).*" + + "?cond_txt_d.{3}([\u4E00-\u9FA5]+).*?date.{3}(.{10}).*?tmp_max.{3}(\\d+).*"); + Calendar c = Calendar.getInstance(); + String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK)); + Integer mwee = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -25,7 +39,72 @@ protected void onCreate(Bundle savedInstanceState) { } public void btnClick(View view) { - new DownloadUpdate().execute(); + String str = "the function is not designed"; + Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show(); + } + + public void btnRefresh(View view) { + boolean success = false; + ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); + NetworkInfo.State state = connManager.getNetworkInfo( + ConnectivityManager.TYPE_WIFI).getState(); // 获取网络连接状态 + if (NetworkInfo.State.CONNECTED == state) { // whether using the WIFI or not + success = true; + } + + state = connManager.getNetworkInfo( + ConnectivityManager.TYPE_MOBILE).getState(); // get the MOBILE net-connection state + if (NetworkInfo.State.CONNECTED == state) { // whether using the GPRS or not + success = true; + } + if (success) { + myUploadData(); + new DownloadUpdate().execute(); + } else { + Toast.makeText(MainActivity.this, "Please check your net-connection", Toast.LENGTH_LONG).show(); + } + } + + private void myUploadData() { + + int year = c.get(Calendar.YEAR); + int month = c.get(Calendar.MONTH) + 1; + int day = c.get(Calendar.DAY_OF_MONTH); + if ("1".equals(mWay)) { + mWay = "SUNDAY"; + mwee = 6; + } else if ("2".equals(mWay)) { + mWay = "MONDAY"; + mwee = 0; + } else if ("3".equals(mWay)) { + mWay = "TUESDAY"; + mwee = 1; + } else if ("4".equals(mWay)) { + mWay = "WEDNESDAY"; + mwee = 2; + } else if ("5".equals(mWay)) { + mWay = "THURSDAY"; + mwee = 3; + } else if ("6".equals(mWay)) { + mWay = "FRIDAY"; + mwee = 4; + } else if ("7".equals(mWay)) { + mWay = "SATURDAY"; + mwee = 5; + } +// String date = null; +// if (day > 9 && month > 9) { +// date = Integer.toString(month) + '/' + Integer.toString(day) + '/' + Integer.toString(year); +// } else if (day < 10 && month > 9) { +// date = Integer.toString(month) + '/' + '0' + Integer.toString(day) + '/' + Integer.toString(year); +// } else if (month < 10 && day > 9) { +// date = '0' + Integer.toString(month) + '/' + Integer.toString(day) + '/' + Integer.toString(year); +// } else { +// date = '0' + Integer.toString(month) + '/' + '0' + Integer.toString(day) + '/' + Integer.toString(year); +// } + ((TextView) findViewById(R.id.head_date)).setText(mWay); +// ((TextView) findViewById(R.id.tv_date)).setText(date); + } @@ -34,9 +113,16 @@ private class DownloadUpdate extends AsyncTask { @Override protected String doInBackground(String... strings) { - String stringUrl = "http://mpianatra.com/Courses/info.txt"; + String stringUrl = "https://free-api.heweather.com/s6/weather/forecast?"; HttpURLConnection urlConnection = null; BufferedReader reader; + String location = "location=chongqing"; + + + String key = "key=411e1bd9c04e4d6c80369ab50bcb63ac"; + + stringUrl = stringUrl + + location + "&" + key; try { URL url = new URL(stringUrl); @@ -81,9 +167,96 @@ protected String doInBackground(String... strings) { } @Override + /* + get the data from the json file which I download from hefeng weather + */ protected void onPostExecute(String temperature) { //Update the temperature displayed - ((TextView) findViewById(R.id.temperature_of_the_day)).setText(temperature); + Matcher m = p.matcher(temperature); + m = p.matcher(temperature); + String location = null; + String temprature = null; + String weather = null; + String date = null; + if (m.find()) { + location = m.group(1); + temprature = m.group(4); + weather = m.group(2); + date = m.group(3); + + } + ImageView wea = findViewById(R.id.img_weather_condition); + TextView text = findViewById(R.id.temperature_of_the_day);// + myChangeWeather(wea,weather);//change the main image of weather + myChangeText(text,temprature);//change the temperature of today + text = findViewById(R.id.tv_location); + myChangeText(text,location); + text = findViewById(R.id.tv_date); + myChangeText(text,date); + + //Change the list below + myChangeList(m); } } + + private void myChangeList(Matcher m) { + String temp = m.group(2); + ImageView wea = findViewById(R.id.day_one); + myChangeWeather(wea,temp); + + temp = ADate[mwee].substring(0,3); + TextView text = findViewById(R.id.text_day_one); + myChangeText(text,temp); + + wea = findViewById(R.id.day_two); + temp = m.group(5); + myChangeWeather(wea,temp); + + temp = ADate[(mwee+1)%7].substring(0,3); + text = findViewById(R.id.text_day_tow); + myChangeText(text,temp); + + temp = m.group(8); + wea = findViewById(R.id.day_thr); + myChangeWeather(wea,temp); + + temp = ADate[(mwee+2)%7].substring(0,3); + text = findViewById(R.id.text_day_thr); + myChangeText(text,temp); + + temp = ADate[(mwee+3)%7].substring(0,3); + text = findViewById(R.id.text_day_fou); + myChangeText(text,temp); + } + + private void myChangeText(TextView viewById, String location) { + viewById.setText(location); + } + + private void myChangeWeather(ImageView wea, String weather) { + switch (weather){ + case "阴": + wea.setImageResource(R.drawable.windy_small); + break; + case"小雨": + wea.setImageResource(R.drawable.rainy_small); + break; + case "晴": + wea.setImageResource(R.drawable.sunny_small); + break; + case"阵雨": + wea.setImageResource(R.drawable.rainy_small); + break; + case "多云": + wea.setImageResource(R.drawable.partly_sunny_small); + break; + case "晴间多云": + wea.setImageResource(R.drawable.partly_sunny_small); + break; + default: + + break; + } + } + } diff --git a/app/src/main/res/drawable-v24/refresh.png b/app/src/main/res/drawable-v24/refresh.png new file mode 100644 index 00000000..6950f552 Binary files /dev/null and b/app/src/main/res/drawable-v24/refresh.png differ diff --git a/app/src/main/res/drawable/blue_bg.png b/app/src/main/res/drawable/blue_bg.png new file mode 100644 index 00000000..0ae5a08d Binary files /dev/null and b/app/src/main/res/drawable/blue_bg.png differ diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index c191667a..468a4a61 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -10,6 +10,7 @@ tools:context="mg.studio.weatherappdesign.MainActivity"> + android:src="@drawable/windy_small" /> + + android:textSize="22sp" + android:id="@+id/textView" /> + + @@ -171,15 +184,18 @@ android:orientation="vertical"> + android:src="@drawable/rainy_small" /> @@ -192,13 +208,15 @@ android:orientation="vertical"> + android:src="@drawable/partly_sunny_small" /> + android:src="@drawable/windy_small" /> @@ -234,15 +254,17 @@ android:orientation="vertical"> + android:src="@drawable/sunny_small" /> diff --git a/readme.md b/readme.md index 3993d0bb..f78f7d8e 100644 --- a/readme.md +++ b/readme.md @@ -1,3 +1,354 @@ +# weather-application-master +the update application explan: + +![demonstration](https://github.com/Hubertyori/weather-application/blob/master/SVID_20180323_225239_20180323230757.gif) + +For the assignment 002 , I added the Net-work state check code and the weather get code which can download the weather information from Hefeng Weather. +First, I added one perminsion in androidManifest.XML + +\ + +to get the permission to access the Network state. + +Then I used the following code to test if the internet are connected. + boolean success = false; + ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); + NetworkInfo.State state = connManager.getNetworkInfo( + ConnectivityManager.TYPE_WIFI).getState(); // 获取网络连接状态 + if (NetworkInfo.State.CONNECTED == state) { // whether using the WIFI or not + success = true; + } + + state = connManager.getNetworkInfo( + ConnectivityManager.TYPE_MOBILE).getState(); // get the MOBILE net-connection state + if (NetworkInfo.State.CONNECTED == state) { // whether using the GPRS or not + success = true; + } + if (success) { + new DownloadUpdate().execute(); + myUploadData(); + } else { + Toast.makeText(MainActivity.this, "Please check your net-connection", Toast.LENGTH_LONG).show(); + + } + +To get the weather information from the internet , I applyed one accont from Hefeng Weather for free. And by the following code, I can download the weather information from the website. + private class DownloadUpdate extends AsyncTask { +private class DownloadUpdate extends AsyncTask { + + + @Override + protected String doInBackground(String... strings) { + String stringUrl = "https://free-api.heweather.com/s6/weather/forecast?"; + HttpURLConnection urlConnection = null; + BufferedReader reader; + String location = "location=chongqing"; + + + String key = "key=411e1bd9c04e4d6c80369ab50bcb63ac"; + + stringUrl = stringUrl + + location + "&" + key; + + try { + URL url = new URL(stringUrl); + + // Create the request to get the information from the server, 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) { + // Mainly needed for debugging + buffer.append(line + "\n"); + } + + if (buffer.length() == 0) { + // Stream was empty. No point in parsing. + return null; + } + //The temperature + return buffer.toString(); + + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (ProtocolException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return null; + } + +And then I need to use the information I got. I changed today's date, the location, the temprature and the day in this week. What's more I changed the weather of day in the future. + + @Override + /* + get the data from the json file which I download from hefeng weather + */ + protected void onPostExecute(String temperature) { + //Update the temperature displayed + Matcher m = p.matcher(temperature); + m = p.matcher(temperature); + String location = null; + String temprature = null; + String weather = null; + String date = null; + if (m.find()) { + location = m.group(1); + temprature = m.group(4); + weather = m.group(2); + date = m.group(3); + + } + ImageView wea = findViewById(R.id.img_weather_condition); + TextView text = findViewById(R.id.temperature_of_the_day);// + myChangeWeather(wea,weather);//change the main image of weather + myChangeText(text,temprature);//change the temperature of today + text = findViewById(R.id.tv_location); + myChangeText(text,location); + text = findViewById(R.id.tv_date); + myChangeText(text,date); + + //Change the list below + myChangeList(m); + } + + private void myChangeList(Matcher m) { + String temp = m.group(2); + ImageView wea = findViewById(R.id.day_one); + myChangeWeather(wea,temp); + + temp = ADate[mwee].substring(0,3); + TextView text = findViewById(R.id.text_day_one); + myChangeText(text,temp); + + wea = findViewById(R.id.day_two); + temp = m.group(5); + myChangeWeather(wea,temp); + + temp = ADate[(mwee+1)%7].substring(0,3); + text = findViewById(R.id.text_day_tow); + myChangeText(text,temp); + + temp = m.group(8); + wea = findViewById(R.id.day_thr); + myChangeWeather(wea,temp); + + temp = ADate[(mwee+2)%7].substring(0,3); + text = findViewById(R.id.text_day_thr); + myChangeText(text,temp); + + temp = ADate[(mwee+3)%7].substring(0,3); + text = findViewById(R.id.text_day_fou); + myChangeText(text,temp); + } + + private void myChangeText(TextView viewById, String location) { + viewById.setText(location); + } + + private void myChangeWeather(ImageView wea, String weather) { + switch (weather){ + case "阴": + wea.setImageResource(R.drawable.windy_small); + break; + case"小雨": + wea.setImageResource(R.drawable.rainy_small); + break; + case "晴": + wea.setImageResource(R.drawable.sunny_small); + break; + case"阵雨": + wea.setImageResource(R.drawable.rainy_small); + break; + case "多云": + wea.setImageResource(R.drawable.partly_sunny_small); + break; + case "晴间多云": + wea.setImageResource(R.drawable.partly_sunny_small); + break; + default: + + break; + } + } + +For the assignment 001 , I did the following changes: +1. I used one image to be the background of TextView like "MON" , by the following code: + +TextView + android:layout_width="48dp" + + android:layout_height="48dp" + + android:gravity="center" + + android:text="mon" + + android:background="@drawable/blue_bg" + + android:textAllCaps="true" + + android:textColor="#909090" + + +background: + +before: + +![MONDAY](https://github.com/Hubertyori/weather-application-master/blob/master/FMT_SX~%5DU8GY6ITTW\)~%7BUAJ.png) + +after: + +![BLUEBG](https://github.com/Hubertyori/weather-application-master/blob/master/%60TVI5%60Q5%5BO_~6%25%7D%7BBE%40%5B1%7BK.png) + +2. I changed the icon that required by changing those code: + +![wind](https://github.com/Hubertyori/weather-application-master/blob/master/UNN%7BNS2OU8M%2492H%7DO1XYLWM.png) + +\ + + + +and I changed the app logo by change this line in AndroidManifest.xml + + +this is what I choosed to be the new app logo: + + ![icon](https://github.com/Hubertyori/weather-application-master/blob/master/weather_icon.png) + +3. I added a imagebutton to refresh the data that required. I used the icon that android stdio are loaded and changed the background color of it, which looks like that: + + ![ICON](https://github.com/Hubertyori/weather-application-master/blob/master/8HJAGO\(%7BS39\(%40DW1TB4CO\)Q.png) + +the code is following: + +The function btnRefresh() is the function that refresh the data. And the code is following: + + + public void btnRefresh(View view) { + new DownloadUpdate().execute(); + myUploadData(); + } + + private void myUploadData() { + + Calendar c = Calendar.getInstance(); + + int year = c.get(Calendar.YEAR); + + int month = c.get(Calendar.MONTH)+1; + + int day = c.get(Calendar.DAY_OF_MONTH); + + + String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK)); + + if("1".equals(mWay)){ + + mWay ="SUNDAY"; + + }else if("2".equals(mWay)){ + + mWay ="MONDAY"; + + }else if("3".equals(mWay)){ + + mWay ="TUESDAY"; + + }else if("4".equals(mWay)){ + + mWay ="WEDNESDAY"; + + }else if("5".equals(mWay)){ + + mWay ="THURSDAY"; + + }else if("6".equals(mWay)){ + + mWay ="FRIDAY"; + + }else if("7".equals(mWay)){ + + mWay ="SATURDAY"; + } + + String date = null; + + if(day>9&&month>9) { + + date = Integer.toString(month) + '/' + Integer.toString(day) + '/' + Integer.toString(year); + + } + + else if(day<10&&month>9){ + + date = Integer.toString(month) + '/' + '0'+Integer.toString(day) + '/' + Integer.toString(year); + + } + + else if(month<10&&day>9){ + + date ='0' + Integer.toString(month) + '/' +Integer.toString(day) + '/' + Integer.toString(year); + + } + + else{ + + date ='0' + Integer.toString(month) + '/' +'0'+Integer.toString(day) + '/' + Integer.toString(year); + + } + + ((TextView) findViewById(R.id.head_date)).setText(mWay); + + ((TextView) findViewById(R.id.tv_date)).setText(date); + + + } + +this the before I pressed the imgbutton: + + ![SUNDAY](https://github.com/Hubertyori/weather-application-master/blob/master/X7%7BEUH%24CB%40OTZCW0%250QD%5D%407.png) + + +and this is after: + + + ![THU](https://github.com/Hubertyori/weather-application-master/blob/master/%25AGIQB%60%7BPEDLK%60%24W2%25SCD\(0.png) + +If there are some bugs that I don't find out please tell me, thank you. + +Yori + +杨译绗 + +20151639 + + +/***************************************************************/ # Weather application ![Weather application](display/weather_app_assign.png) diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml new file mode 100644 index 00000000..8e31e90b --- /dev/null +++ b/src/main/AndroidManifest.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/ic_launcher-web.png b/src/main/ic_launcher-web.png new file mode 100644 index 00000000..9a14ab4b Binary files /dev/null and b/src/main/ic_launcher-web.png differ diff --git a/src/main/java/mg/studio/game/animals/Animations.java b/src/main/java/mg/studio/game/animals/Animations.java new file mode 100644 index 00000000..94195f47 --- /dev/null +++ b/src/main/java/mg/studio/game/animals/Animations.java @@ -0,0 +1,19 @@ +package mg.studio.game.animals; + +import android.content.Context; +import android.support.v7.app.AppCompatActivity; +import android.view.View; +import android.view.animation.Animation; +import android.view.animation.AnimationUtils; + +/** + * Created by Discipulus on 3/24/18. + */ + +public class Animations extends AppCompatActivity{ + + protected void shake(Context context, View viewToAnimate){ + final Animation animShake = AnimationUtils.loadAnimation(context, R.anim.shake); + viewToAnimate.startAnimation(animShake); + } +} diff --git a/src/main/java/mg/studio/game/animals/CongratulationActivity.java b/src/main/java/mg/studio/game/animals/CongratulationActivity.java new file mode 100644 index 00000000..b95e6c09 --- /dev/null +++ b/src/main/java/mg/studio/game/animals/CongratulationActivity.java @@ -0,0 +1,31 @@ +package mg.studio.game.animals; + +import android.content.Intent; +import android.graphics.Typeface; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.view.View; +import android.widget.TextView; + +public class CongratulationActivity extends AppCompatActivity { + + int Score; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_congratulation); + Intent intent = getIntent(); + Score = intent.getIntExtra("score",0); + TextView tex = findViewById(R.id.tv_score); + tex.setText(Integer.toString(Score)); + + } + + public void btnPlayagain(View view) { + Intent intent = new Intent(CongratulationActivity.this, PlayActivity.class); + intent.putExtra("score",0); + intent.putExtra("chances",9); + startActivity(intent); + CongratulationActivity.this.finish(); + } +} diff --git a/src/main/java/mg/studio/game/animals/MainActivity.java b/src/main/java/mg/studio/game/animals/MainActivity.java new file mode 100644 index 00000000..ed52f63b --- /dev/null +++ b/src/main/java/mg/studio/game/animals/MainActivity.java @@ -0,0 +1,50 @@ +package mg.studio.game.animals; + +import android.content.Intent; +import android.support.v7.app.AlertDialog; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.view.View; + +public class MainActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + //Animate the text Animals + new Animations().shake(this, findViewById(R.id.tv_animals)); + + } + + + public void btnStart(View view) { + //to do when the button is clicked + Intent intent = new Intent(MainActivity.this, PlayActivity.class); + intent.putExtra("score",0); + intent.putExtra("chances",9); + startActivity(intent); + } + + public void btnHowTo(View view) { + //to do when the button is clicked + hoToPlay(); + } + + private void hoToPlay() { + // 1. Instantiate an AlertDialog.Builder with its constructor + AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppThemeNoActionBar); + + + // 3. Chain together various setter methods to set the dialog characteristics + builder.setView(R.layout.how_to_play); + + + // 5. Get the AlertDialog from create() the show + AlertDialog dialog = builder.create(); + dialog.show(); + } + + +} diff --git a/src/main/java/mg/studio/game/animals/PlayActivity.java b/src/main/java/mg/studio/game/animals/PlayActivity.java new file mode 100644 index 00000000..a5cf4bd7 --- /dev/null +++ b/src/main/java/mg/studio/game/animals/PlayActivity.java @@ -0,0 +1,210 @@ +package mg.studio.game.animals; + +/** + * Created by Administrator on 2018\3\23 0023. + */ + + +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.os.Build; +import android.os.Vibrator; +import android.support.v7.app.AlertDialog; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.ImageButton; +import android.widget.TextView; + + +import java.util.ArrayList; +import java.util.Random; + +public class PlayActivity extends AppCompatActivity { + + int Score; + int Chances; + String arrayAnimalNames[] = {"bear", "bird", "cat", "elephant", + "fish", "flower", "giraffe", "honey", + "house", "hypo", "kangaroo", "leo", + "lion", "pig", "rhino", "sun", "tiger", + "wolf"}; + TextView tv; + TextView tv_animal_to_find; + private String animalToFind; + private ArrayList imageIdNotToUseAgain; + private ArrayList nameOfImagesUsed; + private ArrayList imageIdNotToUseAgainBlackAndWhite; + private int indexOfTheAnimalNameToFindInImageNotUsedAgain; + private int arrayDrawableImagesIDs[]; + private Vibrator vibrator; + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_play); + Intent intent = getIntent(); + Score = intent.getIntExtra("score", 0); + Chances = intent.getIntExtra("chances", 0); + tv = findViewById(R.id.play_score); + tv.setText(Integer.toString(Score)); + tv = findViewById(R.id.play_chances); + tv.setText(Integer.toString(Chances)); + + tv_animal_to_find = findViewById(R.id.tv_animal_to_find); + + arrayDrawableImagesIDs = new int[]{R.drawable.bear, R.drawable.bird, R.drawable.cat, R.drawable.elephant, + R.drawable.fish, R.drawable.flower, R.drawable.giraffe, R.drawable.honey, + R.drawable.house, R.drawable.hypo, R.drawable.kangaroo, R.drawable.leo, + R.drawable.lion, R.drawable.pig, R.drawable.rhino, R.drawable.sun, R.drawable.tiger, + R.drawable.wolf}; + + + int arrayDrawableImagesBalckWhiteIDs[] = {R.drawable.bear_black, R.drawable.bird_black, R.drawable.cat_black, R.drawable.elephant_black, + R.drawable.fish_black, R.drawable.flower_black, R.drawable.giraffe_black, R.drawable.honey_black, + R.drawable.house_black, R.drawable.hypo_black, R.drawable.kangaroo_black, R.drawable.leo_black, + R.drawable.lion_black, R.drawable.pig_black, R.drawable.rhino_black, R.drawable.sun_black, R.drawable.tiger_black, + R.drawable.wolf_black}; + + ImageButton arrayButtonViews[] = {findViewById(R.id.btn_top_left), findViewById(R.id.btn_top_right), + findViewById(R.id.btn_bottom_left), findViewById(R.id.btn_bottom_right)}; + + + imageIdNotToUseAgain = new ArrayList<>(); //Use this this to keep track of the image already used + imageIdNotToUseAgainBlackAndWhite = new ArrayList<>(); + nameOfImagesUsed = new ArrayList<>(); //Keep track of the image names used + + //Get four random image to display in the app + for (ImageButton btnView : arrayButtonViews) { + int indexOfWhatToUse; + while (true) { + indexOfWhatToUse = (int) (Math.random() * (arrayDrawableImagesIDs.length - 1)); + Log.d(getPackageName(), "indexOfWhatToUse: " + indexOfWhatToUse); + if (!imageIdNotToUseAgain.contains(indexOfWhatToUse)) { + break; + } + } + + imageIdNotToUseAgain.add(indexOfWhatToUse); + imageIdNotToUseAgainBlackAndWhite.add(arrayDrawableImagesBalckWhiteIDs[indexOfWhatToUse]); + + btnView.setImageResource(arrayDrawableImagesIDs[indexOfWhatToUse]); + nameOfImagesUsed.add(arrayAnimalNames[indexOfWhatToUse]); + + } + + //From the four names, choose one that the use needs to find + indexOfTheAnimalNameToFindInImageNotUsedAgain = new Random().nextInt(3) + 0; + animalToFind = nameOfImagesUsed.get(indexOfTheAnimalNameToFindInImageNotUsedAgain); + tv_animal_to_find.setText(animalToFind); + + + } + + + public void btnClick(View view) { + + Log.d(getPackageName(), "btnClick(): " + view.getTag().toString()); + Chances--; + TextView tv = (TextView) findViewById(R.id.play_chances); + tv.setText(Integer.toString(Chances)); + if (animalToFind.equals(nameOfImagesUsed.get(Integer.valueOf(view.getTag().toString())))) { + Log.d(getPackageName(), "btnClick(): " + nameOfImagesUsed.get(Integer.valueOf(view.getTag().toString()))); + + //ToDo 1 : Inform the user the choice was correct + //by adding the score + //Todo 2 : Keep track of the score as long as the application is not closed + Score++; + tv = (TextView) findViewById(R.id.play_score); + tv.setText(Integer.toString(Score)); + + //Todo 3 : Generate a new challenge (at least 7 challenges needed before ending the game) + //Todo 4 : Show a congratulating message if the user finishes all the challenges + if (Chances == 0) { + Intent intent = new Intent(PlayActivity.this, CongratulationActivity.class); + intent.putExtra("score", Score); + startActivity(intent); + PlayActivity.this.finish(); + } else { + Intent intent = new Intent(PlayActivity.this, PlayActivity.class); + intent.putExtra("score", Score); + intent.putExtra("chances", Chances); + startActivity(intent); + PlayActivity.this.finish(); + } + + + } else { + if (Chances == 0) { + Intent intent = new Intent(PlayActivity.this, CongratulationActivity.class); + intent.putExtra("score", Score); + startActivity(intent); + PlayActivity.this.finish(); + } else { + //Vibrate the phone + vibrate(); + + //Change the image into its black and white version + ImageButton imageButton = (ImageButton) view; + imageButton.setImageResource(imageIdNotToUseAgainBlackAndWhite.get(Integer.valueOf(view.getTag().toString()))); + + //Animate the views to attract the user`s attention + new Animations().shake(this, view); + new Animations().shake(this, tv_animal_to_find); + + } + } + } + + + private void vibrate() { + // Get an instance of Vibrator from the current Context + vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); + //Check if the device has a vibrator + if (vibrator.hasVibrator()) { + Log.d(getPackageName(), "Device hasVibrator (): " + vibrator.hasVibrator()); + //Check if the vibrator has amplitude control + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + Log.d(getPackageName(), "hasAmplitudeControl (): " + vibrator.hasAmplitudeControl()); + } + // Vibrate the device for 400 milliseconds + vibrator.vibrate(400); + } + } + + @Override + public void onBackPressed() { + // 1. Instantiate an AlertDialog.Builder with its constructor + AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppThemeDialog); + + + // 2. Set an icon, in this case, we use the animal to find as the icon + builder.setIcon(arrayDrawableImagesIDs[imageIdNotToUseAgain.get(indexOfTheAnimalNameToFindInImageNotUsedAgain)]); + + // 3. Chain together various setter methods to set the dialog characteristics + builder.setMessage("Do you want to exit the game?") + .setTitle(getTitle()); + + // 4. Add the buttons + builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + // User clicked OK button, end the App + finishAffinity(); + } + }); + builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + // User clicked NO, cancel the dialog + dialog.cancel(); + } + }); + + // 5. Get the AlertDialog from create() the show + AlertDialog dialog = builder.create(); + dialog.show(); + + } +} \ No newline at end of file diff --git a/src/main/res/anim/shake.xml b/src/main/res/anim/shake.xml new file mode 100644 index 00000000..6787f388 --- /dev/null +++ b/src/main/res/anim/shake.xml @@ -0,0 +1,18 @@ + + + + \ No newline at end of file diff --git a/src/main/res/drawable-v24/ic_launcher_foreground.xml b/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 00000000..c7bd21db --- /dev/null +++ b/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + diff --git a/src/main/res/drawable/bear.png b/src/main/res/drawable/bear.png new file mode 100644 index 00000000..a8c9d48f Binary files /dev/null and b/src/main/res/drawable/bear.png differ diff --git a/src/main/res/drawable/bear_black.png b/src/main/res/drawable/bear_black.png new file mode 100644 index 00000000..23107a43 Binary files /dev/null and b/src/main/res/drawable/bear_black.png differ diff --git a/src/main/res/drawable/bg.png b/src/main/res/drawable/bg.png new file mode 100644 index 00000000..cf78670b Binary files /dev/null and b/src/main/res/drawable/bg.png differ diff --git a/src/main/res/drawable/bg_blue.png b/src/main/res/drawable/bg_blue.png new file mode 100644 index 00000000..03523c92 Binary files /dev/null and b/src/main/res/drawable/bg_blue.png differ diff --git a/src/main/res/drawable/bird.png b/src/main/res/drawable/bird.png new file mode 100644 index 00000000..751dfc2c Binary files /dev/null and b/src/main/res/drawable/bird.png differ diff --git a/src/main/res/drawable/bird_black.png b/src/main/res/drawable/bird_black.png new file mode 100644 index 00000000..e3bb8eff Binary files /dev/null and b/src/main/res/drawable/bird_black.png differ diff --git a/src/main/res/drawable/cat.png b/src/main/res/drawable/cat.png new file mode 100644 index 00000000..b2925b9f Binary files /dev/null and b/src/main/res/drawable/cat.png differ diff --git a/src/main/res/drawable/cat_black.png b/src/main/res/drawable/cat_black.png new file mode 100644 index 00000000..0b7d10e5 Binary files /dev/null and b/src/main/res/drawable/cat_black.png differ diff --git a/src/main/res/drawable/elephant.png b/src/main/res/drawable/elephant.png new file mode 100644 index 00000000..4eef967c Binary files /dev/null and b/src/main/res/drawable/elephant.png differ diff --git a/src/main/res/drawable/elephant_black.png b/src/main/res/drawable/elephant_black.png new file mode 100644 index 00000000..835b52b5 Binary files /dev/null and b/src/main/res/drawable/elephant_black.png differ diff --git a/src/main/res/drawable/fish.png b/src/main/res/drawable/fish.png new file mode 100644 index 00000000..61ac3547 Binary files /dev/null and b/src/main/res/drawable/fish.png differ diff --git a/src/main/res/drawable/fish_black.png b/src/main/res/drawable/fish_black.png new file mode 100644 index 00000000..9be0cb17 Binary files /dev/null and b/src/main/res/drawable/fish_black.png differ diff --git a/src/main/res/drawable/flower.png b/src/main/res/drawable/flower.png new file mode 100644 index 00000000..5087eb95 Binary files /dev/null and b/src/main/res/drawable/flower.png differ diff --git a/src/main/res/drawable/flower_black.png b/src/main/res/drawable/flower_black.png new file mode 100644 index 00000000..b8ab847e Binary files /dev/null and b/src/main/res/drawable/flower_black.png differ diff --git a/src/main/res/drawable/giraffe.png b/src/main/res/drawable/giraffe.png new file mode 100644 index 00000000..70b7f3d8 Binary files /dev/null and b/src/main/res/drawable/giraffe.png differ diff --git a/src/main/res/drawable/giraffe_black.png b/src/main/res/drawable/giraffe_black.png new file mode 100644 index 00000000..6734e7fd Binary files /dev/null and b/src/main/res/drawable/giraffe_black.png differ diff --git a/src/main/res/drawable/honey.png b/src/main/res/drawable/honey.png new file mode 100644 index 00000000..63dee33a Binary files /dev/null and b/src/main/res/drawable/honey.png differ diff --git a/src/main/res/drawable/honey_black.png b/src/main/res/drawable/honey_black.png new file mode 100644 index 00000000..87f72068 Binary files /dev/null and b/src/main/res/drawable/honey_black.png differ diff --git a/src/main/res/drawable/house.png b/src/main/res/drawable/house.png new file mode 100644 index 00000000..3610422f Binary files /dev/null and b/src/main/res/drawable/house.png differ diff --git a/src/main/res/drawable/house_black.png b/src/main/res/drawable/house_black.png new file mode 100644 index 00000000..8737168a Binary files /dev/null and b/src/main/res/drawable/house_black.png differ diff --git a/src/main/res/drawable/hypo.png b/src/main/res/drawable/hypo.png new file mode 100644 index 00000000..749ad54f Binary files /dev/null and b/src/main/res/drawable/hypo.png differ diff --git a/src/main/res/drawable/hypo_black.png b/src/main/res/drawable/hypo_black.png new file mode 100644 index 00000000..ec70ab2a Binary files /dev/null and b/src/main/res/drawable/hypo_black.png differ diff --git a/src/main/res/drawable/ic_launcher_background.xml b/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 00000000..d5fccc53 --- /dev/null +++ b/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/res/drawable/kangaroo.png b/src/main/res/drawable/kangaroo.png new file mode 100644 index 00000000..9e27bac5 Binary files /dev/null and b/src/main/res/drawable/kangaroo.png differ diff --git a/src/main/res/drawable/kangaroo_black.png b/src/main/res/drawable/kangaroo_black.png new file mode 100644 index 00000000..9dcd7f6c Binary files /dev/null and b/src/main/res/drawable/kangaroo_black.png differ diff --git a/src/main/res/drawable/leo.png b/src/main/res/drawable/leo.png new file mode 100644 index 00000000..45d3e293 Binary files /dev/null and b/src/main/res/drawable/leo.png differ diff --git a/src/main/res/drawable/leo_black.png b/src/main/res/drawable/leo_black.png new file mode 100644 index 00000000..f84e33a6 Binary files /dev/null and b/src/main/res/drawable/leo_black.png differ diff --git a/src/main/res/drawable/lion.png b/src/main/res/drawable/lion.png new file mode 100644 index 00000000..f4ce332d Binary files /dev/null and b/src/main/res/drawable/lion.png differ diff --git a/src/main/res/drawable/lion_black.png b/src/main/res/drawable/lion_black.png new file mode 100644 index 00000000..1530d19f Binary files /dev/null and b/src/main/res/drawable/lion_black.png differ diff --git a/src/main/res/drawable/pig.png b/src/main/res/drawable/pig.png new file mode 100644 index 00000000..473b3c1d Binary files /dev/null and b/src/main/res/drawable/pig.png differ diff --git a/src/main/res/drawable/pig_black.png b/src/main/res/drawable/pig_black.png new file mode 100644 index 00000000..582511bd Binary files /dev/null and b/src/main/res/drawable/pig_black.png differ diff --git a/src/main/res/drawable/rhino.png b/src/main/res/drawable/rhino.png new file mode 100644 index 00000000..129f1e8a Binary files /dev/null and b/src/main/res/drawable/rhino.png differ diff --git a/src/main/res/drawable/rhino_black.png b/src/main/res/drawable/rhino_black.png new file mode 100644 index 00000000..b2efcacb Binary files /dev/null and b/src/main/res/drawable/rhino_black.png differ diff --git a/src/main/res/drawable/selector.xml b/src/main/res/drawable/selector.xml new file mode 100644 index 00000000..b2f4ab21 --- /dev/null +++ b/src/main/res/drawable/selector.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/src/main/res/drawable/sun.png b/src/main/res/drawable/sun.png new file mode 100644 index 00000000..24743d2d Binary files /dev/null and b/src/main/res/drawable/sun.png differ diff --git a/src/main/res/drawable/sun_black.png b/src/main/res/drawable/sun_black.png new file mode 100644 index 00000000..93a2c522 Binary files /dev/null and b/src/main/res/drawable/sun_black.png differ diff --git a/src/main/res/drawable/text_selector.xml b/src/main/res/drawable/text_selector.xml new file mode 100644 index 00000000..3b59b532 --- /dev/null +++ b/src/main/res/drawable/text_selector.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/res/drawable/tiger.png b/src/main/res/drawable/tiger.png new file mode 100644 index 00000000..947dfef7 Binary files /dev/null and b/src/main/res/drawable/tiger.png differ diff --git a/src/main/res/drawable/tiger_black.png b/src/main/res/drawable/tiger_black.png new file mode 100644 index 00000000..8b8328a2 Binary files /dev/null and b/src/main/res/drawable/tiger_black.png differ diff --git a/src/main/res/drawable/wolf.png b/src/main/res/drawable/wolf.png new file mode 100644 index 00000000..0448fcf6 Binary files /dev/null and b/src/main/res/drawable/wolf.png differ diff --git a/src/main/res/drawable/wolf_black.png b/src/main/res/drawable/wolf_black.png new file mode 100644 index 00000000..b88c5934 Binary files /dev/null and b/src/main/res/drawable/wolf_black.png differ diff --git a/src/main/res/font/font_one.TTF b/src/main/res/font/font_one.TTF new file mode 100644 index 00000000..fe4328b6 Binary files /dev/null and b/src/main/res/font/font_one.TTF differ diff --git a/src/main/res/layout/activity_congratulation.xml b/src/main/res/layout/activity_congratulation.xml new file mode 100644 index 00000000..25a2a2b3 --- /dev/null +++ b/src/main/res/layout/activity_congratulation.xml @@ -0,0 +1,58 @@ + + + + + + +