Android Open Source - Android-Apps Main Activity






From Project

Back to project page Android-Apps.

License

The source code is released under:

Apache License

If you think the Android project Android-Apps 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

package com.kniezrec.xbmcgear.presentation;
/*from  ww  w  .j  a  va  2  s .c o m*/
import android.app.AlertDialog;
import android.app.DialogFragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kniezrec.xbmcgear.R;
import com.kniezrec.xbmcgear.connection.Connection;
import com.kniezrec.xbmcgear.player.Kodi;
import com.kniezrec.xbmcgear.preferences.Host;
import com.kniezrec.xbmcgear.preferences.HostsDataSource;
import com.kniezrec.xbmcgear.preferences.SharedPreferencesUtil;

public class MainActivity extends ActionBarActivity {

    public static final int CONNECTED_GEAR = 100;
    public static final int DISCONNECTED_GEAR = 200;
    public static final int CONNECTED_XBMC = 300;
    public static final int DISCONNECTED_XBMC = 400;

    public static final String INT_EXTRA = "com.kniezrec.xbmcgear.presentation.MainActivity.INT";
    public static final String INTENT_FILTER = "com.kniezrec.xbmcgear.presentation.MainActivity";
    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            MainActivity.this.receivedBroadcast(intent);
        }
    };
    private Connection mConnection;
    private HostsDataSource mDataSource;
    private ImageButton expandBtn;
    private AnimationManager xbmcCardAnimation;
    private TextView mGearItem;
    private TextView mXbmcItem;
    private TextView profileNameLabel;
    private TextView hostInfoLabel;
    private LinearLayout infoLayout;
    private TextView hostVersion;
    private CardView xbmcCardView;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
        mGearItem = (TextView) findViewById(R.id.gearConnectionLabel);
        mXbmcItem = (TextView) findViewById(R.id.xbmcConnectionLabel);
        xbmcCardView = (CardView) findViewById(R.id.card_xbmc);
        expandBtn = (ImageButton) findViewById(R.id.expandButton);
        infoLayout = (LinearLayout) findViewById(R.id.infoGroup);
        profileNameLabel = (TextView) findViewById(R.id.lab_profile_name);
        hostInfoLabel = (TextView) findViewById(R.id.lab_host_info);
        hostVersion = (TextView) findViewById(R.id.lab_host_version);

        if (toolbar != null) {
            setSupportActionBar(toolbar);
            getSupportActionBar().setTitle(R.string.connections);
        }

        mDataSource = HostsDataSource.getInstance();
        mConnection = Connection.getInstance();
        if (SharedPreferencesUtil.isFirstRun()) {
            showWelcomeDialog();
        }
        if (SharedPreferencesUtil.isHostDefined()) {
            mConnection.setHost(SharedPreferencesUtil.getPreferences());
        }


        xbmcCardAnimation = new AnimationManager();
        xbmcCardAnimation.setLayoutView(infoLayout);
        xbmcCardAnimation.setButtonView(expandBtn);

        xbmcCardView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        xbmcCardAnimation.setCardView(xbmcCardView);
                        xbmcCardView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }
                });
        expandBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                xbmcCardAnimation.animate();
            }
        });
        mConnection.checkWiFi();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDataSource.close();
    }

    @Override
    public void onResume() {
        super.onResume();
        mGearItem.setText(gearConnectionStatus());
        mXbmcItem.setText(xbmcConnectionStatus());
        if (mConnection.isConnectedToXBMC()) {
            setExpandBtnVisible(true);
        } else {
            setExpandBtnVisible(false);
        }
        IntentFilter filter = new IntentFilter();
        filter.addAction(INTENT_FILTER);
        this.registerReceiver(this.mBroadcastReceiver, filter);
        mConnection.checkWiFi();
    }

    private void showWelcomeDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Intent intent = new Intent(MainActivity.this,
                        AutoConfigurationActivity.class);
                MainActivity.this.startActivity(intent);
                MainActivity.this.overridePendingTransition(R.anim.slide,
                        R.anim.sout);
                dialog.dismiss();
            }
        });

        AlertDialog dialog = builder.create();
        dialog.setMessage(getResources()
                .getString(R.string.welcome_body_device));
        dialog.setTitle(getResources().getString(R.string.welcome_title));
        dialog.show();
    }

    private String gearConnectionStatus() {
        if (mConnection.isConnectedToGear()) {
            return getString(R.string.connected);
        } else {
            return getString(R.string.disconnected);
        }
    }

    private void setExpandBtnVisible(boolean isVisible) {
        if (isVisible) {
            int orientation = getResources().getConfiguration().orientation;

            if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                infoLayout.setVisibility(View.VISIBLE);
            } else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                expandBtn.setVisibility(View.VISIBLE);
                expandBtn.setEnabled(true);
            }

            Host h = mConnection.getCurrentActiveHost();
            if (h != null) {
                profileNameLabel.setText(h.getProfileName());
                hostInfoLabel.setText(h.getIp() + ":" + h.getPort());
                Kodi instanceKodi = mConnection.getPlayer().kodi;
                StringBuilder sb = new StringBuilder();
                sb.append(getString(R.string.version));
                sb.append(" ");

                if (instanceKodi != null) {
                    sb.append(mConnection.getPlayer().kodi.toString());
                } else {
                    sb.append(getString(android.R.string.unknownName));
                }
                hostVersion.setText(sb.toString());
            }
        } else {
            xbmcCardAnimation.collapseIfNeeded();
            expandBtn.clearAnimation();
            expandBtn.setEnabled(false);
            expandBtn.setVisibility(View.INVISIBLE);
            infoLayout.setVisibility(View.GONE);
        }
    }

    private String xbmcConnectionStatus() {
        if (mConnection.isConnectedToXBMC()) {
            return getString(R.string.connected);
        } else {
            return getString(R.string.disconnected);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        this.unregisterReceiver(this.mBroadcastReceiver);
    }

    private void receivedBroadcast(Intent i) {
        int state = i.getIntExtra(INT_EXTRA, 0);
        switch (state) {
            case CONNECTED_GEAR:
                if (mGearItem != null) {
                    mGearItem.setText(getString(R.string.connected));
                }

                break;
            case DISCONNECTED_GEAR:
                if (mGearItem != null) {
                    mGearItem.setText(getString(R.string.disconnected));
                }
                break;

            case CONNECTED_XBMC:
                if (mXbmcItem != null) {
                    mXbmcItem.setText(getString(R.string.connected));
                }
                setExpandBtnVisible(true);
                break;
            case DISCONNECTED_XBMC:
                if (mXbmcItem != null) {
                    mXbmcItem.setText(getString(R.string.disconnected));
                }
                setExpandBtnVisible(false);
                break;
            default:
                break;
        }
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.gallery_theme) {
            Intent intent = new Intent(MainActivity.this, InstanceActivity.class);
            MainActivity.this.startActivity(intent);
            this.overridePendingTransition(R.anim.slide, R.anim.sout);
            return true;
        } else if (id == R.id.gear_theme_instances) {
            DialogFragment newFragment = StyleDialogFragment.newInstance();
            newFragment.show(getFragmentManager(), "dialog");

            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}




Java Source Code List

com.kniezrec.remoterecorder.Communication.java
com.kniezrec.remoterecorder.MainServiceConnection.java
com.kniezrec.remoterecorder.MainService.java
com.kniezrec.remoterecorder.RequestType.java
com.kniezrec.remoterecorder.Request.java
com.kniezrec.voiceremote2.BSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremote2.BSeriesSender.java
com.kniezrec.voiceremote2.CSeriesButtons.java
com.kniezrec.voiceremote2.CSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremote2.CSeriesSender.java
com.kniezrec.voiceremote2.CommandsFragment.java
com.kniezrec.voiceremote2.Commands.java
com.kniezrec.voiceremote2.Discovery.java
com.kniezrec.voiceremote2.FSeriesButtons.java
com.kniezrec.voiceremote2.Group.java
com.kniezrec.voiceremote2.HelpFragment.java
com.kniezrec.voiceremote2.HostnamePreference.java
com.kniezrec.voiceremote2.KeyCodeSender.java
com.kniezrec.voiceremote2.ListActionsFragment.java
com.kniezrec.voiceremote2.MainActivity.java
com.kniezrec.voiceremote2.MainFragment.java
com.kniezrec.voiceremote2.Mapper.java
com.kniezrec.voiceremote2.MyExpandableListAdapter.java
com.kniezrec.voiceremote2.NewActionEdit.java
com.kniezrec.voiceremote2.NewActionSingleEdit.java
com.kniezrec.voiceremote2.NewAction.java
com.kniezrec.voiceremote2.RemoteButton.java
com.kniezrec.voiceremote2.SenderFactory.java
com.kniezrec.voiceremote2.Sender.java
com.kniezrec.voiceremote2.SettingsActivity.java
com.kniezrec.voiceremote2.TextSender.java
com.kniezrec.voiceremotefree.BSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremotefree.BSeriesSender.java
com.kniezrec.voiceremotefree.CSeriesButtons.java
com.kniezrec.voiceremotefree.CSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremotefree.CSeriesSender.java
com.kniezrec.voiceremotefree.Commands.java
com.kniezrec.voiceremotefree.Discovery.java
com.kniezrec.voiceremotefree.FSeriesButtons.java
com.kniezrec.voiceremotefree.HelpActivity.java
com.kniezrec.voiceremotefree.HostnamePreference.java
com.kniezrec.voiceremotefree.KeyCodeSender.java
com.kniezrec.voiceremotefree.ListActionsActivity.java
com.kniezrec.voiceremotefree.MainActivity.java
com.kniezrec.voiceremotefree.Mapper.java
com.kniezrec.voiceremotefree.NewActionEdit.java
com.kniezrec.voiceremotefree.NewActionSingleEdit.java
com.kniezrec.voiceremotefree.NewAction.java
com.kniezrec.voiceremotefree.RemoteButton.java
com.kniezrec.voiceremotefree.SenderFactory.java
com.kniezrec.voiceremotefree.Sender.java
com.kniezrec.voiceremotefree.Setings.java
com.kniezrec.voiceremotefree.SettingsActivity.java
com.kniezrec.voiceremotefree.TextSender.java
com.kniezrec.xbmcgear.connection.AndroidApplication.java
com.kniezrec.xbmcgear.connection.Connection.java
com.kniezrec.xbmcgear.connection.GearJSON.java
com.kniezrec.xbmcgear.connection.JSONRPCRequest.java
com.kniezrec.xbmcgear.connection.JSONRequestFactory.java
com.kniezrec.xbmcgear.connection.NSDResolve.java
com.kniezrec.xbmcgear.connection.NSDSearch.java
com.kniezrec.xbmcgear.connection.ProviderConnection.java
com.kniezrec.xbmcgear.connection.ProviderService.java
com.kniezrec.xbmcgear.connection.ResponseParser.java
com.kniezrec.xbmcgear.connection.WakeOnLan.java
com.kniezrec.xbmcgear.player.Kodi.java
com.kniezrec.xbmcgear.player.Player.java
com.kniezrec.xbmcgear.player.Playlist.java
com.kniezrec.xbmcgear.player.Song.java
com.kniezrec.xbmcgear.player.Video.java
com.kniezrec.xbmcgear.preferences.HostTable.java
com.kniezrec.xbmcgear.preferences.Host.java
com.kniezrec.xbmcgear.preferences.HostsDataSource.java
com.kniezrec.xbmcgear.preferences.HostsDatabaseHelper.java
com.kniezrec.xbmcgear.preferences.SharedPreferencesUtil.java
com.kniezrec.xbmcgear.presentation.AnimationManager.java
com.kniezrec.xbmcgear.presentation.AutoConfigurationActivity.java
com.kniezrec.xbmcgear.presentation.HostSetActivity.java
com.kniezrec.xbmcgear.presentation.InstanceActivity.java
com.kniezrec.xbmcgear.presentation.MainActivity.java
com.kniezrec.xbmcgear.presentation.StyleDialogFragment.java
com.kniezrec.xbmcgear.presentation.ViewMode.java
com.uraroji.garage.android.lame.SimpleLame.java
com.uraroji.garage.android.mp3recvoice.RecMicToMp3.java
de.quist.samy.remocon.Base64.java
de.quist.samy.remocon.Base64.java
de.quist.samy.remocon.ConnectionDeniedException.java
de.quist.samy.remocon.ConnectionDeniedException.java
de.quist.samy.remocon.Key.java
de.quist.samy.remocon.Key.java
de.quist.samy.remocon.Loggable.java
de.quist.samy.remocon.Loggable.java
de.quist.samy.remocon.RemoteReader.java
de.quist.samy.remocon.RemoteReader.java
de.quist.samy.remocon.RemoteSession.java
de.quist.samy.remocon.RemoteSession.java