Android Open Source - muckebox-android Wizard Activity






From Project

Back to project page muckebox-android.

License

The source code is released under:

Apache License

If you think the Android project muckebox-android 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 2013 Karsten Patzwaldt// ww  w .java2  s  .co m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.muckebox.android.ui.activity;

import java.io.IOException;

import javax.net.ssl.SSLException;

import org.apache.http.auth.AuthenticationException;
import org.json.JSONException;
import org.json.JSONObject;
import org.muckebox.android.R;
import org.muckebox.android.net.ApiHelper;
import org.muckebox.android.ui.utils.HeightEvaluator;
import org.muckebox.android.ui.widgets.ImageViewRotater;
import org.muckebox.android.utils.Preferences;

import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.CheckBox;
import android.widget.TextView;

public class WizardActivity extends Activity {
    private static final int SPEC = MeasureSpec.makeMeasureSpec(4096, MeasureSpec.AT_MOST);
    
    MenuItem mDoneItem;
    MenuItem mTestItem;
    
    View mExtraContainer;
    int mExtraContainerHeight;
    
    View mExpandButton;
    
    TextView mServerText;
    TextView mPasswordText;
    TextView mPortText;
    CheckBox mSslCheckbox;
    
    TextView mResultText;
    
    private class TestConnectionTask extends AsyncTask<Void, Void, Integer> {
        @Override
        protected void onPreExecute() {
            mTestItem.setActionView(
                ImageViewRotater.getRotatingImageView(
                        WizardActivity.this, R.layout.action_view_refresh));
        }

        @Override
        protected Integer doInBackground(Void... params) {
            try {
                JSONObject response = ApiHelper.callApiForObject("ping");
                
                if (! response.getBoolean("pong"))
                    throw new JSONException("mountpoint");
            } catch (SSLException e) {
                return R.string.wizard_error_ssl;
            } catch (AuthenticationException e) {
                return R.string.wizard_error_password;
            } catch (IOException e) {
                return R.string.wizard_error_connection;
            } catch (JSONException e) {
                return R.string.wizard_error_mount_point;
            }
            
            return R.string.wizard_success;
        }
        
        @Override
        protected void onPostExecute(Integer result) {
            mResultText.setText(getText(result));
            mTestItem.getActionView().clearAnimation();
            mTestItem.setActionView(null);
            
            if (result == R.string.wizard_success) {
                mDoneItem.setVisible(true);
            } 
        }
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_wizard);
        
        mExtraContainer = findViewById(R.id.wizard_extra_container);
        mExtraContainer.measure(SPEC, SPEC);
        mExtraContainerHeight = mExtraContainer.getMeasuredHeight();
        
        LayoutParams params = mExtraContainer.getLayoutParams();
        params.height = 0;
        mExtraContainer.setLayoutParams(params);
        
        mExpandButton = findViewById(R.id.wizard_expand_button);
        mExpandButton.setOnClickListener(
            new OnClickListener() {
                public void onClick(View v) {
                    AnimatorSet animatorSet = new AnimatorSet();
                    
                    animatorSet.play(ValueAnimator.ofObject(
                        new HeightEvaluator(mExtraContainer), 0, mExtraContainerHeight));
                    animatorSet.play(ValueAnimator.ofObject(
                        new HeightEvaluator(mExpandButton), mExpandButton.getHeight(), 0));
                    
                    animatorSet.start();
                }
            });
        
        mServerText = (TextView) findViewById(R.id.wizard_server_text);
        mPasswordText = (TextView) findViewById(R.id.wizard_server_password);
        mPortText = (TextView) findViewById(R.id.wizard_port_text);
        mSslCheckbox = (CheckBox) findViewById(R.id.wizard_ssl_enabled);
        
        mResultText = (TextView) findViewById(R.id.wizard_result_text);
        
        readFromPreferences();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        
        inflater.inflate(R.menu.wizard, menu);
        
        mDoneItem = menu.findItem(R.id.wizard_action_done);
        mTestItem = menu.findItem(R.id.wizard_action_test);
        
        mDoneItem.setVisible(false);
        
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item == mTestItem) {
            writeToPreferences();
        
            new TestConnectionTask().execute();
            
            return true;
        } else if (item == mDoneItem) {
            Preferences.setWizardCompleted();
            
            Intent intent = new Intent(this, MuckeboxActivity.class);
            startActivity(intent);
            
            return true;
        }
        
        return false;
    }
    
    private void readFromPreferences() {
        mServerText.setText(Preferences.getServerAddress());
        mPasswordText.setText(Preferences.getServerPassword());
        mPortText.setText(Integer.toString(Preferences.getServerPort()));
        mSslCheckbox.setChecked(Preferences.getSSLEnabled());
    }
    
    private void writeToPreferences() {
        Preferences.setServerAddress(mServerText.getText().toString());
        Preferences.setServerPassword(mPasswordText.getText().toString());
        Preferences.setServerPort(Integer.parseInt(mPortText.getText().toString()));
        Preferences.setSSLEnabled(mSslCheckbox.isChecked());
    }
}




Java Source Code List

org.muckebox.android.Muckebox.java
org.muckebox.android.audio.PlayerListener.java
org.muckebox.android.audio.PlayerService.java
org.muckebox.android.audio.PlayerWrapper.java
org.muckebox.android.db.DownloadEntryCursor.java
org.muckebox.android.db.MuckeboxContract.java
org.muckebox.android.db.MuckeboxDbHelper.java
org.muckebox.android.db.MuckeboxProvider.java
org.muckebox.android.db.PlaylistHelper.java
org.muckebox.android.net.ApiHelper.java
org.muckebox.android.net.DownloadCatchupRunnable.java
org.muckebox.android.net.DownloadListener.java
org.muckebox.android.net.DownloadRunnable.java
org.muckebox.android.net.DownloadServer.java
org.muckebox.android.net.DownloadService.java
org.muckebox.android.net.MuckeboxHttpClient.java
org.muckebox.android.net.PreannounceTask.java
org.muckebox.android.net.RefreshAlbumsTask.java
org.muckebox.android.net.RefreshArtistsAlbumsTask.java
org.muckebox.android.net.RefreshArtistsTask.java
org.muckebox.android.net.RefreshHelper.java
org.muckebox.android.net.RefreshTask.java
org.muckebox.android.net.RefreshTracksTask.java
org.muckebox.android.ui.activity.MuckeboxActivity.java
org.muckebox.android.ui.activity.WizardActivity.java
org.muckebox.android.ui.fragment.AlbumListFragment.java
org.muckebox.android.ui.fragment.ArtistListFragment.java
org.muckebox.android.ui.fragment.DownloadListFragment.java
org.muckebox.android.ui.fragment.DrawerFragment.java
org.muckebox.android.ui.fragment.PlayerFragment.java
org.muckebox.android.ui.fragment.SettingsFragment.java
org.muckebox.android.ui.fragment.TrackListFragment.java
org.muckebox.android.ui.utils.ExpandableCursorAdapter.java
org.muckebox.android.ui.utils.HeightEvaluator.java
org.muckebox.android.ui.utils.ImageButtonHelper.java
org.muckebox.android.ui.utils.NavigationListener.java
org.muckebox.android.ui.utils.ScrollYAnimator.java
org.muckebox.android.ui.utils.TimeFormatter.java
org.muckebox.android.ui.widgets.ImageViewRotater.java
org.muckebox.android.ui.widgets.LiveSearchView.java
org.muckebox.android.ui.widgets.RefreshableListFragment.java
org.muckebox.android.ui.widgets.SearchableListFragment.java
org.muckebox.android.utils.BufferUtils.java
org.muckebox.android.utils.CacheCleaner.java
org.muckebox.android.utils.Preferences.java
org.muckebox.android.utils.RemoteControlReceiver.java