Back to project page Android-Apps.
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.
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); } }