Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.23 KB | None | 0 0
  1. package com.example.myapplication;
  2.  
  3. import android.app.Dialog;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.support.design.widget.FloatingActionButton;
  7. import android.support.design.widget.Snackbar;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.support.v7.widget.DialogTitle;
  10. import android.support.v7.widget.Toolbar;
  11. import android.util.Log;
  12. import android.view.View;
  13. import android.view.Menu;
  14. import android.view.MenuItem;
  15. import android.widget.Button;
  16. import android.widget.Toast;
  17.  
  18. import com.google.android.gms.common.ConnectionResult;
  19. import com.google.android.gms.common.GoogleApiAvailability;
  20. import com.google.android.gms.common.api.GoogleApiActivity;
  21.  
  22. public class MainActivity extends AppCompatActivity {
  23.  
  24. private static final String TAG = "MainActivity";
  25.  
  26. private static final int ERROR_DIALOGUE_REQUEST = 9001;
  27.  
  28. @Override
  29. protected void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.activity_main);
  32. Toolbar toolbar = findViewById(R.id.toolbar);
  33. setSupportActionBar(toolbar);
  34.  
  35. FloatingActionButton fab = findViewById(R.id.fab);
  36. fab.setOnClickListener(new View.OnClickListener() {
  37. @Override
  38. public void onClick(View view) {
  39. Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  40. .setAction("Action", null).show();
  41.  
  42. if(isservicesOK()){
  43. init();
  44. }
  45. }
  46. });
  47. }
  48.  
  49. //one button to navigate to the map
  50. private void init(){
  51. Button btnMap = (Button) findViewById(R.id.btnMap);
  52. btnMap.setOnClickListener(new View.OnClickListener(){
  53. @Override
  54. public void onClick(View view){
  55. //Log.i("App","mine");
  56. //Toast.makeText(getApplicationContext(),"its working", Toast.LENGTH_SHORT).show();
  57. Intent i = new Intent(MainActivity.this, MapActivity.class);
  58. startActivity(i);
  59.  
  60. }
  61. });
  62.  
  63. }
  64. //if the map doesn't work, these are the following commands:
  65. public boolean isservicesOK(){
  66. Log.d(TAG, "isservicesOK: checking google services ");
  67.  
  68. int available = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(MainActivity.this);
  69.  
  70. if(available == ConnectionResult.SUCCESS){
  71. Log.d(TAG, "isservicesOK: Google play service is working ");
  72. return true;
  73. } //everything is fine the user can make map requests
  74. else if(GoogleApiAvailability.getInstance().isUserResolvableError(available)){
  75. //error occur but we can resolve it. if they have the wrong OS
  76. Log.d(TAG, "isservicesOK: error but we can fix it ");
  77. Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(MainActivity.this, available, ERROR_DIALOGUE_REQUEST);
  78. dialog.show();
  79. }else{//you cant do anything now buddy!
  80. Toast.makeText(this, "you cant make map requests", Toast.LENGTH_SHORT ).show();
  81. }
  82. return false;
  83. }
  84.  
  85. @Override
  86. public boolean onCreateOptionsMenu(Menu menu) {
  87. // Inflate the menu; this adds items to the action bar if it is present.
  88. getMenuInflater().inflate(R.menu.menu_main, menu);
  89. return true;
  90. }
  91.  
  92. @Override
  93. public boolean onOptionsItemSelected(MenuItem item) {
  94. // Handle action bar item clicks here. The action bar will
  95. // automatically handle clicks on the Home/Up button, so long
  96. // as you specify a parent activity in AndroidManifest.xml.
  97. int id = item.getItemId();
  98.  
  99. //noinspection SimplifiableIfStatement
  100. if (id == R.id.action_settings) {
  101. return true;
  102. }
  103.  
  104. return super.onOptionsItemSelected(item);
  105. }
  106. }
  107.  
  108. package com.example.myapplication;
  109.  
  110. import android.Manifest;
  111. import android.content.pm.PackageManager;
  112. import android.os.Bundle;
  113. import android.support.annotation.NonNull;
  114. import android.support.annotation.Nullable;
  115. import android.support.v4.app.ActivityCompat;
  116. import android.support.v4.content.ContextCompat;
  117. import android.support.v7.app.AppCompatActivity;
  118. import android.util.Log;
  119. import android.widget.Toast;
  120. import android.support.v4.app.FragmentActivity;
  121. import com.google.android.gms.maps.GoogleMap;
  122. import com.google.android.gms.maps.OnMapReadyCallback;
  123. import com.google.android.gms.maps.SupportMapFragment;
  124.  
  125. public class MapActivity extends AppCompatActivity implements
  126. OnMapReadyCallback{
  127.  
  128. @Override
  129. public void onMapReady(GoogleMap googleMap){
  130. Toast.makeText(this, "map is ready" , Toast.LENGTH_SHORT).show();
  131. Log.d(TAG, "onMapReady: map is ready Beta");
  132. mMap = googleMap;
  133. }
  134.  
  135. private static final String TAG = "MapActivity";
  136.  
  137. private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
  138. private static final String COURSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
  139. private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234;
  140.  
  141. //VARIABLES
  142. private Boolean mLocationPermissionGranted = false;
  143. private GoogleMap mMap;
  144.  
  145.  
  146. @Override
  147. protected void onCreate(@Nullable Bundle savedInstanceState) {
  148. super.onCreate(savedInstanceState);
  149. setContentView(R.layout.activity_map);
  150. //check for location permission
  151. getLocationPermission();
  152. }
  153.  
  154. private void initMap()
  155. {
  156. Log.d(TAG, "initMap: initialsing map");
  157. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
  158. mapFragment.getMapAsync(MapActivity.this);
  159. }
  160. private void getLocationPermission(){
  161. Log.d(TAG, "getLocationPermission: getting location permission");
  162. String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION,
  163. Manifest.permission.ACCESS_COARSE_LOCATION};
  164.  
  165. if(ContextCompat.checkSelfPermission(this.getApplicationContext(),
  166. FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
  167. if(ContextCompat.checkSelfPermission(this.getApplicationContext(),
  168. COURSE_LOCATION)== PackageManager.PERMISSION_GRANTED){
  169. mLocationPermissionGranted = true;
  170.  
  171. }else{
  172. ActivityCompat.requestPermissions(this,
  173. permissions,
  174. LOCATION_PERMISSION_REQUEST_CODE);
  175. }
  176. }
  177. }
  178. @Override
  179. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
  180. Log.d(TAG, "onRequestPermissionsResult: called yaar");
  181. mLocationPermissionGranted = false;
  182.  
  183. switch(requestCode){
  184. case LOCATION_PERMISSION_REQUEST_CODE: {
  185. if(grantResults.length > 0){
  186. for(int j = 0; j < grantResults.length; j++){
  187. if(grantResults[j] != PackageManager.PERMISSION_GRANTED){
  188. mLocationPermissionGranted = false;
  189. Log.d(TAG, "onRequestPermissionsResult: permission failed");
  190. return;
  191. }
  192. }
  193. Log.d(TAG, "onRequestPermissionsResult: permission granted");
  194. mLocationPermissionGranted = true;
  195. //initialize the map
  196. initMap();
  197. }
  198. }
  199. }
  200. }
  201.  
  202. }
  203.  
  204. apply plugin: 'com.android.application'
  205.  
  206. android {
  207. compileSdkVersion 28
  208.  
  209. defaultConfig {
  210. applicationId "com.example.myapplication"
  211. minSdkVersion 15
  212. targetSdkVersion 28
  213. versionCode 1
  214. versionName "1.0"
  215. testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  216. }
  217. buildTypes {
  218. release {
  219. minifyEnabled false
  220. proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
  221. }
  222. }
  223. }
  224.  
  225. dependencies {
  226. implementation fileTree(include: ['*.jar'], dir: 'libs')
  227. //noinspection GradleCompatible
  228. implementation 'com.android.support:appcompat-v7:28.0.0'
  229. implementation 'com.android.support.constraint:constraint-layout:1.1.3'
  230. implementation 'com.android.support:design:28.0.0'
  231. testImplementation 'junit:junit:4.12'
  232. androidTestImplementation 'com.android.support.test:runner:1.0.2'
  233. androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
  234. //google play services
  235. api 'com.google.android.gms:play-services-maps:16.1.0'
  236. implementation 'com.android.support:support-v4:28.0.0'
  237. }
  238.  
  239. 2019-03-21 10:44:51.332 25541-25541/com.example.myapplication
  240. I/e.myapplicatio: Not late-enabling -Xcheck:jni (already on)
  241. 2019-03-21 10:44:51.603 25541-25541/com.example.myapplication
  242. W/e.myapplicatio: Unexpected CPU variant for X86 using defaults: x86
  243. 2019-03-21 10:44:51.759 25541-25541/com.example.myapplication
  244. I/e.myapplicatio: The ClassLoaderContext is a special shared library.
  245. 2019-03-21 10:44:52.252 25541-25541/com.example.myapplication
  246. W/e.myapplicatio: Accessing hidden method Landroid/view/View;-
  247. >computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z
  248. (light greylist, reflection)
  249. 2019-03-21 10:44:52.252 25541-25541/com.example.myapplication
  250. W/e.myapplicatio: Accessing hidden method Landroid/view/ViewGroup;-
  251. >makeOptionalFitsSystemWindows()V (light greylist, reflection)
  252. 2019-03-21 10:44:52.560 25541-25541/com.example.myapplication
  253. D/OpenGLRenderer: Skia GL Pipeline
  254. 2019-03-21 10:44:52.652 25541-25566/com.example.myapplication
  255. I/ConfigStore:
  256. android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay
  257. retrieved: 0
  258. 2019-03-21 10:44:52.652 25541-25566/com.example.myapplication
  259. I/OpenGLRenderer: Initialized EGL, version 1.4
  260. 2019-03-21 10:44:52.652 25541-25566/com.example.myapplication
  261. D/OpenGLRenderer: Swap behavior 1
  262. 2019-03-21 10:44:52.652 25541-25566/com.example.myapplication
  263. W/OpenGLRenderer: Failed to choose config with
  264. EGL_SWAP_BEHAVIOR_PRESERVED,
  265. retrying without...
  266. 2019-03-21 10:44:52.652 25541-25566/com.example.myapplication
  267. D/OpenGLRenderer: Swap behavior 0
  268. 2019-03-21 10:44:52.670 25541-25566/com.example.myapplication
  269. D/EGL_emulation: eglCreateContext: 0xea564e80: maj 3 min 0 rcv 3
  270. 2019-03-21 10:44:52.688 25541-25566/com.example.myapplication
  271. D/EGL_emulation: eglMakeCurrent: 0xea564e80: ver 3 0 (tinfo 0xea4ce580)
  272. 2019-03-21 10:44:52.865 25541-25566/com.example.myapplication
  273. D/EGL_emulation: eglMakeCurrent: 0xea564e80: ver 3 0 (tinfo 0xea4ce580)
  274.  
  275. GoogleMap map = ((SupportMapFragment) getFragmentManager()
  276. .findFragmentById(R.id.map)).getMap();
  277.  
  278. <fragment
  279. android:id="@+id/map"
  280. android:layout_width="match_parent"
  281. android:layout_height="match_parent"
  282. class="com.google.android.gms.maps.SupportMapFragment" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement