protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource   = Resource.Layout.Toolbar;

            LocalNotificationsImplementation.NotificationIconId = Resource.Drawable.app_logo;

            App.ScreenWidth  = (Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density);
            App.ScreenHeight = (Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density);

            // This event fires when the ServiceConnection lets the client (our App class) know that
            // the Service is connected. We use this event to start updating the UI with location
            // updates from the Service
            AppAndroid.Current.LocationServiceConnected += (sender, e) =>
            {
                Log.Debug(TAG, "ServiceConnected Event Raised");
                // notifies us of location changes from the system
                AppAndroid.Current.LocationService.LocationChanged += HandleLocationChanged;
                //notifies us of user changes to the location provider (ie the user disables or enables GPS)
                AppAndroid.Current.LocationService.ProviderDisabled += HandleProviderDisabled;
                AppAndroid.Current.LocationService.ProviderEnabled  += HandleProviderEnabled;
                // notifies us of the changing status of a provider (ie GPS no longer available)
                AppAndroid.Current.LocationService.StatusChanged += HandleStatusChanged;
            };

            base.OnCreate(savedInstanceState);

            UserDialogs.Init(this);
            CrossCurrentActivity.Current.Init(this, savedInstanceState);

            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());

            //Start the location service:
            if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessFineLocation) == (int)Permission.Granted)
            {
                Log.Debug(TAG, "User already has granted permission.");
                AppAndroid.StartLocationService();
            }
            else
            {
                Log.Debug(TAG, "Have to request permission from the user. ");
                RequestLocationPermission();
            }
            // Check if the Camera permission is already available.
            if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.Camera) != (int)Permission.Granted)
            {
                // Camera permission has not been granted
                RequestCameraPermission();
            }
            else
            {
                // Camera permissions is already available, show the camera preview.
                Log.Info(TAG, "CAMERA permission has already been granted. Displaying camera preview.");
            }
        }
        //public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        //{
        //    var layout = FindViewById(Android.Resource.Id.Content);
        //    if (requestCode == RC_REQUEST_LOCATION_PERMISSION)
        //    {
        //        if (grantResults.Length == 1 && grantResults[0] == Permission.Granted)
        //        {
        //            Log.Debug(TAG, "User granted permission for location.");
        //            AppAndroid.StartLocationService();
        //        }
        //        else
        //        {
        //            Log.Warn(TAG, "User did not grant permission for the location.");
        //        }
        //    }
        //    if (requestCode == REQUEST_CAMERA)
        //    {
        //        // Received permission result for camera permission.
        //        Log.Info(TAG, "Received response for Camera permission request.");

        //        // Check if the only required permission has been granted
        //        if (grantResults.Length == 1 && grantResults[0] == Permission.Granted)
        //        {
        //            // Camera permission has been granted, preview can be displayed
        //            Log.Info(TAG, "CAMERA permission has now been granted. Showing preview.");
        //            Snackbar.Make(layout, "CAMERA permission has now been granted. Showing preview.", Snackbar.LengthShort).Show();
        //        }
        //        else
        //        {
        //            Log.Info(TAG, "CAMERA permission was NOT granted.");
        //            Snackbar.Make(layout, "CAMERA permission was NOT granted.", Snackbar.LengthShort).Show();
        //        }
        //    }
        //    //else if (requestCode == REQUEST_CONTACTS)
        //    //{
        //    //    Log.Info(TAG, "Received response for contact permissions request.");

        //    //    // We have requested multiple permissions for contacts, so all of them need to be
        //    //    // checked.
        //    //    if (PermissionUtil.VerifyPermissions(grantResults))
        //    //    {
        //    //        // All required permissions have been granted, display contacts fragment.
        //    //        Snackbar.Make(layout, "Contacts permissions were granted.", Snackbar.LengthShort).Show();
        //    //    }
        //    //    else
        //    //    {
        //    //        Log.Info(TAG, "Contacts permissions were NOT granted.");
        //    //        Snackbar.Make(layout, "Contacts permissions were NOT granted.", Snackbar.LengthShort).Show();
        //    //    }
        //    //}
        //    else
        //    {
        //        PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        //        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        //        //base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        //    }

        //}

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
        {
            var layout = FindViewById(Android.Resource.Id.Content);

            if (requestCode == RC_REQUEST_LOCATION_PERMISSION)
            {
                if (grantResults.Length == 1 && grantResults[0] == Permission.Granted)
                {
                    Log.Debug(TAG, "User granted permission for location.");
                    AppAndroid.StartLocationService();
                }
                else
                {
                    Log.Warn(TAG, "User did not grant permission for the location.");
                }
            }
            if (requestCode == REQUEST_CAMERA)
            {
                // Received permission result for camera permission.
                Log.Info(TAG, "Received response for Camera permission request.");

                // Check if the only required permission has been granted
                if (grantResults.Length == 1 && grantResults[0] == Permission.Granted)
                {
                    // Camera permission has been granted, preview can be displayed
                    Log.Info(TAG, "CAMERA permission has now been granted. Showing preview.");
                    Snackbar.Make(layout, "CAMERA permission has now been granted. Showing preview.", Snackbar.LengthShort).Show();
                }
                else
                {
                    Log.Info(TAG, "CAMERA permission was NOT granted.");
                    Snackbar.Make(layout, "CAMERA permission was NOT granted.", Snackbar.LengthShort).Show();
                }
            }
            else
            {
                base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        }
Exemplo n.º 3
0
 static AppAndroid()
 {
     Current = new AppAndroid();
 }