com.android.projectz.teamrocket.thebusapp.settings.SettingGeneralActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.android.projectz.teamrocket.thebusapp.settings.SettingGeneralActivity.java

Source

package com.android.projectz.teamrocket.thebusapp.settings;

/*
 Copyright (C) 2016-2017  TeamRocket
    
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.afollestad.materialdialogs.color.CircleView;
import com.afollestad.materialdialogs.color.ColorChooserDialog;
import com.afollestad.materialdialogs.internal.ThemeSingleton;
import com.afollestad.materialdialogs.util.DialogUtils;
import com.android.projectz.teamrocket.thebusapp.IntroApp;
import com.android.projectz.teamrocket.thebusapp.R;
import com.android.projectz.teamrocket.thebusapp.activities.MainActivity;
import com.android.projectz.teamrocket.thebusapp.adapters.CustomListSettingOther;
import com.android.projectz.teamrocket.thebusapp.util.SharedPreferencesUtils;

/**
 * SettingGeneralActivity:
 * questa classe serve per impostare 2 cose principali (versione 0.6):
 * 1. tema
 * 2. data
 * --
 * tema: visualizzer 2 custom dialog che attraverso l'input dell'utente setter il tema all'app
 * bisogna fare dei controlli su tutte gli Intent perch altrimenti setta il tema al Fragment
 * corrent (creare metodi per il controllo delle sharedpreferences)
 * data: da la possibilit di scaricare in locale il db degli orari e altre impostazioni per l'I/O dell'app
 *
 * @author simone98dm
 * @data 05/12/2016
 */

public class SettingGeneralActivity extends AppCompatActivity {

    /* inizio della activity */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.setTheme(getResources().getIdentifier(SharedPreferencesUtils.getSelectedTheme(this), "style",
                getPackageName()));
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_general);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarGeneralSetting);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(R.string.title_activity_general_setting);
        getSupportActionBar().setSubtitle(R.string.subtitle_activity_general_setting);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    /**
     * Metodo che serve a far visualizzare la lista con le relative personalizzazioni all'interno della schermata
     * <p>
     * ho deciso di fare ci in modo tale da richiamare il metodo quando la lingua viene cambiata, cosi la vista 
     * aggiornata con la lingua selezionata
     */
    @Override
    protected void onStart() {
        super.onStart();
        String[] settingText = { getResources().getString(R.string.setting_theme),
                getResources().getString(R.string.setting_instrictions) };
        String[] settingSubtext = {
                SharedPreferencesUtils.getSelectedTheme(SettingGeneralActivity.this).equals("AppTheme") ? "Light"
                        : "Dark",
                getResources().getString(R.string.short_instruction) };
        Resources res = getResources();
        Drawable[] settingImg = { ContextCompat.getDrawable(this, R.drawable.ic_theme_chooser),
                ContextCompat.getDrawable(this, R.drawable.ic_instruction) };
        CustomListSettingOther adapter = new CustomListSettingOther(SettingGeneralActivity.this, settingText,
                settingSubtext, settingImg);
        ListView list = (ListView) findViewById(R.id.general_setting_list);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                switch (position) {
                case 0:
                    new MaterialDialog.Builder(SettingGeneralActivity.this).title(R.string.dialog_title_theme)
                            .items(R.array.theme)
                            .itemsCallbackSingleChoice(
                                    SharedPreferencesUtils.getSelectedTheme(SettingGeneralActivity.this)
                                            .equals("AppTheme") ? 0 : 1,
                                    new MaterialDialog.ListCallbackSingleChoice() {
                                        @Override
                                        public boolean onSelection(MaterialDialog dialog, View view, int which,
                                                CharSequence text) {
                                            try {
                                                SharedPreferencesUtils.setSelectedTheme(SettingGeneralActivity.this,
                                                        text.equals("Light") ? "AppTheme" : "ThemeDark");
                                                reload();
                                            } catch (Exception e) {
                                                e.printStackTrace();
                                                SharedPreferencesUtils.setSelectedLanguage(
                                                        SettingGeneralActivity.this, "AppTheme");
                                            }
                                            return true;
                                        }
                                    })
                            .positiveText(R.string.dialog_ok_button).show();

                    break;
                case 1:
                    Intent inst = new Intent(SettingGeneralActivity.this, IntroApp.class);
                    startActivity(inst);
                    SettingGeneralActivity.this.overridePendingTransition(R.anim.anim_slide_in_right,
                            R.anim.anim_slide_out_right);
                    break;
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            this.finish();
            SettingGeneralActivity.this.overridePendingTransition(R.anim.anim_slide_in_right,
                    R.anim.anim_slide_out_right);
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            this.finish();
            SettingGeneralActivity.this.overridePendingTransition(R.anim.anim_slide_in_right,
                    R.anim.anim_slide_out_right);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void reload() {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

}