Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson7 #8

Open
wants to merge 5 commits into
base: Lesson6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@
.externalNativeBuild
.cxx
local.properties
/app/google-services.json
/app/src/debug/res/values/google_maps_api.xml
10 changes: 6 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
plugins {
id 'com.android.application'
}
apply plugin: 'com.android.application'

android {
compileSdkVersion 29
Expand Down Expand Up @@ -50,8 +48,12 @@ dependencies {
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.firebase:firebase-messaging:20.1.0'
implementation 'com.google.android.gms:play-services-maps:17.0.0'
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
}

apply plugin: 'com.google.gms.google-services'
24 changes: 24 additions & 0 deletions app/src/debug/res/values/google_maps_api.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<resources>
<!--
TODO: Before you run your application, you need a Google Maps API key.

To get one, follow this link, follow the directions and press "Create" at the end:

https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=13:01:02:CD:00:63:D4:18:8F:50:45:51:E9:B2:4B:D5:3D:87:D7:C8%3Bcom.example.android2

You can also add your credentials to an existing key, using these values:

Package name:
com.example.android2

SHA-1 certificate fingerprint:
13:01:02:CD:00:63:D4:18:8F:50:45:51:E9:B2:4B:D5:3D:87:D7:C8

Alternatively, follow the directions here:
https://developers.google.com/maps/documentation/android/start#get-key

Once you have your key (it starts with "AIza"), replace the "google_maps_key"
string in this file.
-->
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AIzaSyAXrwjIMeJFlU9MOPpOrPZ7KGz-wx5Avy8</string>
</resources>
29 changes: 28 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android2">

<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the "MyLocation" functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand All @@ -13,7 +22,25 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name=".activity.SecondActivity"></activity>
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />

<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

<activity android:name=".activity.SecondActivity" />
<activity
android:name=".activity.MainActivity"
android:label="@string/app_name">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.example.android2;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private int messageId = 0;

public MyFirebaseMessagingService() {}

@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
String title = remoteMessage.getNotification().getTitle();
if (title == null) {
title = "Push message";
}

String text = remoteMessage.getNotification().getBody();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "2")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(text);

NotificationManager notificationManager = (NotificationManager) this.getSystemService(
Context.NOTIFICATION_SERVICE
);
if (notificationManager != null) {
notificationManager.notify(messageId++, builder.build());
}
}

@Override
public void onNewToken(@NonNull String s) {
Log.i("MyLog", s);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.android2;

public interface OnMapFragmentListener {
void onMapFragment();
}
5 changes: 5 additions & 0 deletions app/src/main/java/com/example/android2/OpenWeather.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@
public interface OpenWeather {
@GET("data/2.5/weather")
Call<WeatherData> loadWeather(@Query("q") String city, @Query("appid") String keyApi);

@GET("data/2.5/weather")
Call<WeatherData> loadWeatherByCoord(@Query("lat") String latitudeLine,
@Query("lon") String longitudeLine,
@Query("appid") String keyApi);
}
21 changes: 19 additions & 2 deletions app/src/main/java/com/example/android2/Settings.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.example.android2;

import androidx.room.Room;

public class Settings {
private static Settings instance = null;

Expand Down Expand Up @@ -33,6 +31,9 @@ public static Settings getInstance() {
};
private int currentIndexOfCity = 0;

private double latitude = 0f;
private double longitude = 0f;

public boolean isDarkThemeFlag() {
return darkTheme;
}
Expand All @@ -52,4 +53,20 @@ public int getCurrentIndexOfCity() {
public void setCurrentIndexOfCity(int currentIndexOfCity) {
this.currentIndexOfCity = currentIndexOfCity;
}

public double getLatitude() {
return latitude;
}

public void setLatitude(double latitude) {
this.latitude = latitude;
}

public double getLongitude() {
return longitude;
}

public void setLongitude(double longitude) {
this.longitude = longitude;
}
}
14 changes: 14 additions & 0 deletions app/src/main/java/com/example/android2/activity/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package com.example.android2.activity;

import android.Manifest;
import android.app.Activity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.Menu;

import com.example.android2.fragment.MapsFragment;
import com.example.android2.fragment.AuthorFragment;
import com.example.android2.fragment.HistoryFragment;
import com.example.android2.fragment.MainFragment;
Expand All @@ -22,6 +30,7 @@

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.core.app.ActivityCompat;
import androidx.core.view.GravityCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
Expand All @@ -40,6 +49,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
private MainFragment mainFragment;
private AuthorFragment authorFragment;
private HistoryFragment historyFragment;
private MapsFragment mapsFragment;

private final BroadcastReceiver batteryReceiver = new BatteryReceiver();
private final BroadcastReceiver networkReceiver = new NetworkReceiver();
Expand All @@ -52,6 +62,7 @@ protected void onCreate(Bundle savedInstanceState) {
mainFragment = new MainFragment();
authorFragment = new AuthorFragment();
historyFragment = new HistoryFragment();
mapsFragment = new MapsFragment();

toolbar = initToolbar();

Expand Down Expand Up @@ -148,6 +159,9 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
case R.id.nav_author:
changeFragment(authorFragment);
break;
case R.id.nav_map :
changeFragment(mapsFragment);
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
Expand Down
Loading