com.rizal.lovins.smartkasir.activity.SettingsActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.rizal.lovins.smartkasir.activity.SettingsActivity.java

Source

package com.rizal.lovins.smartkasir.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.GridView;

import com.rizal.lovins.smartkasir.R;
import com.rizal.lovins.smartkasir.adapter.CustomGridViewSettingAdapter;
import com.rizal.lovins.smartkasir.util.PreferenceUtil;

public class SettingsActivity extends AppCompatActivity {
    Toolbar toolbar;
    GridView androidGridView;

    String[] gridViewString = { "About", "Developer", "License", "Google Maps", "Location" };

    int[] gridViewImageId = { R.drawable.ic_settings_about, R.drawable.ic_settings_about,
            R.drawable.ic_settings_license, R.drawable.ic_settings_maps, R.drawable.ic_settings_maps };

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        setTitle("Settings");

        WebView mWebView = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.getSettings().setBuiltInZoomControls(false);
        mWebView.getSettings().setDisplayZoomControls(false);
        mWebView.loadUrl("file:///android_asset/license/settings.html");

        CustomGridViewSettingAdapter adapterViewAndroid = new CustomGridViewSettingAdapter(SettingsActivity.this,
                gridViewString, gridViewImageId);
        androidGridView = (GridView) findViewById(R.id.grid_view_image_text);
        androidGridView.setAdapter(adapterViewAndroid);
        androidGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                case 0:
                    startActivity(new Intent(SettingsActivity.this, AboutActivity.class));
                    break;
                case 1:
                    startActivity(new Intent(SettingsActivity.this, DeveloperActivity.class));
                    break;
                case 2:
                    startActivity(new Intent(SettingsActivity.this, LicenseActivity.class));
                    break;
                case 3:
                    startActivity(new Intent(SettingsActivity.this, MapsActivity.class));
                    break;
                case 4:
                    location();
                    break;
                }
            }
        });

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.settings_menu_logout:
            menuLogout();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }

    }

    public void menuLogout() {
        AlertDialog.Builder builder = new AlertDialog.Builder(SettingsActivity.this);
        builder.setTitle("Logout");
        builder.setMessage("Apakah Anda yakin ingin keluar?");
        builder.setCancelable(true);
        builder.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                PreferenceUtil.logout(SettingsActivity.this);
                startActivity(new Intent(SettingsActivity.this, LoginActivity.class));
                finishAffinity();
            }
        });

        builder.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.settings_menu, menu);
        return true;
    }

    private void location() {
        LocationManager mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this,
                android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(this,
                        android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        Location location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {
            new AlertDialog.Builder(SettingsActivity.this).setTitle("Location")
                    .setMessage("lat: " + location.getLatitude() + ", long: " + location.getLongitude())
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).show();
        }
    }
}