Android Open Source - event-viewer Settings Activity






From Project

Back to project page event-viewer.

License

The source code is released under:

Copyright (c) 2014, Delta Controls Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are...

If you think the Android project event-viewer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/* Copyright (c) 2014, Delta Controls Inc.
All rights reserved.//from   w  ww .jav a  2 s  . co  m

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this 
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this 
list of conditions and the following disclaimer in the documentation and/or other 
materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may 
be used to endorse or promote products derived from this software without specific 
prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
package com.deltacontrols.eventviewer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.deltacontrols.eventviewer.service.ScheduleEventNotifications;
import com.deltacontrols.eweb.support.api.EwebConnection;
import com.deltacontrols.eweb.support.api.EwebConnection.CONNECTION_STATUS;
import com.deltacontrols.eweb.support.api.FetchJSON;
import com.deltacontrols.eweb.support.api.FetchJSON.Result;
import com.deltacontrols.eweb.support.interfaces.GenericCallback;

/**
 * Settings/Login page
 */
public class SettingsActivity extends Activity {

    /**
     * Extends an ArrayList to allow search by string
     */
    private class searchableStringArrayList extends ArrayList<String> {
        private static final long serialVersionUID = 2526795240635839538L; // Autogenerated

        public searchableStringArrayList(List<String> list) {
            super(list);
        }

        public int getIndexFor(int iStr) {
            String str = String.valueOf(iStr);
            return getIndexFor(str);
        }

        public int getIndexFor(String str) {
            for (int i = 0; i < this.size(); i++) {
                if (this.get(i).equals(str)) {
                    return i;
                }
            }
            return -1;
        }
    }

    // ------------------------------------------------------------------------------
    // Outlets
    // ------------------------------------------------------------------------------
    private TextView mUrl;
    private TextView mUser;
    private TextView mPass;
    private Spinner mRefreshTimeSpinner;
    private Button mLogin;

    // ------------------------------------------------------------------------------
    // Properties
    // ------------------------------------------------------------------------------
    private static int mRefreshTimeDefaultIndex = 3;        // 5 mins.
    private searchableStringArrayList mRefreshTimeValues;   // Custom class; see above

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        mUrl = (TextView) findViewById(R.id.url);
        mUser = (TextView) findViewById(R.id.user_name);
        mPass = (TextView) findViewById(R.id.password);
        mRefreshTimeSpinner = (Spinner) findViewById(R.id.refreshTimeSpinner);
        mRefreshTimeValues = new searchableStringArrayList(Arrays.asList(this.getResources().getStringArray(R.array.refresh_time_array_values)));

        // Login button
        mLogin = (Button) findViewById(R.id.loginButton);
        mLogin.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView url = (TextView) findViewById(R.id.url);
                TextView user = (TextView) findViewById(R.id.user_name);
                TextView pass = (TextView) findViewById(R.id.password);

                mLogin.setEnabled(false);

                final String urlStr = url.getText().toString();
                final String userStr = user.getText().toString();
                final String passStr = pass.getText().toString();

                // If all empty, set demo and continue
                if (urlStr.isEmpty() && userStr.isEmpty() && passStr.isEmpty()) {
                    LoginInfo.deleteStoredLoginInfo(SettingsActivity.this);
                    // Launch main activity
                    Intent intent = new Intent(App.getContext(), MainActivity.class);
                    startActivity(intent);
                    finish();
                }
                else {
                    // Else, attempt login
                    final EwebConnection eweb = new EwebConnection();
                    // Use eweb connect to verify the validity of the information before we proceed.
                    // Note: the Service makes use of it's own EwebConnection instance; this is used only for immediate UI feedback.
                    eweb.connect("http://" + urlStr, userStr, passStr, new GenericCallback<FetchJSON.Result>() {
                        @Override
                        public void onCallback(Result result) {
                            if (eweb.connectionStatus() == CONNECTION_STATUS.OK) {
                                LoginInfo login = new LoginInfo(urlStr, userStr, passStr, true);
                                LoginInfo.setLoginInfo(App.getContext(), login);

                                // Start up the service immediately.
                                ScheduleEventNotifications.startServiceRepeating(App.getContext(), 0, login.refreshSeconds);

                                // Launch main activity
                                Intent intent = new Intent(App.getContext(), MainActivity.class);
                                startActivity(intent);
                                finish();
                            }
                            else if (eweb.connectionStatus() == CONNECTION_STATUS.ERROR_INVALID_APPS_LICENSE) {
                                Toast.makeText(SettingsActivity.this, getString(R.string.login_activity_server_invalid_apps_license), Toast.LENGTH_LONG).show();
                            }
                            else {
                                Toast.makeText(SettingsActivity.this, getString(R.string.login_activity_failed_to_login), Toast.LENGTH_LONG).show();
                            }

                            mLogin.setEnabled(true);
                        }
                    });
                }
            }
        });

        // Setup login information and auto sync if the user is currently active.
        LoginInfo login = LoginInfo.getLoginInfo(this);
        mUrl.setText(login.url);
        mUser.setText(login.username);
        mPass.setText(login.password);

        // Set refresh time; do lookup of value to get text index
        int refreshIndex = mRefreshTimeValues.getIndexFor(login.refreshSeconds);
        if (refreshIndex == -1) {
            refreshIndex = mRefreshTimeDefaultIndex;
        }
        mRefreshTimeSpinner.setSelection(refreshIndex);

        // Enable/disable based on if user is logged in / active
        if (login.active) {
            mLogin.setVisibility(View.GONE);
            mUrl.setEnabled(false);
            mUser.setEnabled(false);
            mPass.setEnabled(false);
        }
        else {
            mLogin.setVisibility(View.VISIBLE);
            mUrl.setEnabled(true);
            mUser.setEnabled(true);
            mPass.setEnabled(true);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        try {
            // Get refresh seconds, and if they are different than the stored settings, update and restart service.
            String refreshTime = mRefreshTimeValues.get(mRefreshTimeSpinner.getSelectedItemPosition());

            int refreshInt = Integer.parseInt(refreshTime);
            LoginInfo login = LoginInfo.getLoginInfo(App.getContext());
            if (login.refreshSeconds != refreshInt) {
                login.refreshSeconds = refreshInt;
                LoginInfo.setLoginInfo(App.getContext(), login);

                // Restart service if user is active (ie. we are logged in)
                if (LoginInfo.storedUserIsActive(this)) {
                    ScheduleEventNotifications.stopServiceRepeating(this);
                    ScheduleEventNotifications.startServiceRepeating(this, 0, refreshInt);
                }
            }
        } catch (Exception e) {
            Toast.makeText(this, getString(R.string.login_activity_problem_saving_settings), Toast.LENGTH_LONG).show();
        }
    }
}




Java Source Code List

com.deltacontrols.eventviewer.App.java
com.deltacontrols.eventviewer.EventItemAdapter.java
com.deltacontrols.eventviewer.GroupSpinnerItemAdapter.java
com.deltacontrols.eventviewer.LauncherActivity.java
com.deltacontrols.eventviewer.LoginInfo.java
com.deltacontrols.eventviewer.MainActivity.java
com.deltacontrols.eventviewer.MoreIndicator.java
com.deltacontrols.eventviewer.ServiceWrapper.java
com.deltacontrols.eventviewer.SettingsActivity.java
com.deltacontrols.eventviewer.Triangle.java
com.deltacontrols.eventviewer.controls.EventDetailView.java
com.deltacontrols.eventviewer.service.EventCache.java
com.deltacontrols.eventviewer.service.EventNotificationsService.java
com.deltacontrols.eventviewer.service.ScheduleEventNotificationsBootReceiver.java
com.deltacontrols.eventviewer.service.ScheduleEventNotifications.java