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 w w w . j av a 2 s.c o m import android.content.Intent; import android.net.nsd.NsdServiceInfo; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.Toolbar; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.ArrayAdapter; import android.widget.ListView; import com.kniezrec.xbmcgear.R; import com.kniezrec.xbmcgear.connection.Connection; import com.kniezrec.xbmcgear.connection.NSDSearch; import com.kniezrec.xbmcgear.preferences.Host; import com.kniezrec.xbmcgear.preferences.HostsDataSource; import com.kniezrec.xbmcgear.preferences.SharedPreferencesUtil; import java.lang.ref.WeakReference; import java.util.ArrayList; public class AutoConfigurationActivity extends ActionBarActivity implements AdapterView.OnItemClickListener { public static final String TO_EDIT = "TO_EDIT"; public static final String ADD_MANUAL = "MANUL_ADD"; public static final String SHOW_HOME = "SHOW_HOME"; private final int SHOW_HOST = 20; private ArrayAdapter<Host> adapter; private ArrayList<Host> list; private NSDSearch nsdSearch; private ListView listView; private int clicked; private int checkedPos; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.autoconfiguration_activity); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_gearstyle); if (toolbar != null) { setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } //boolean isBackEnable = getIntent().getBooleanExtra(SHOW_HOME, false); list = new ArrayList<Host>(); adapter = new ArrayAdapter<Host>(this, android.R.layout.simple_list_item_single_choice, list); listView = (ListView) findViewById(R.id.listAutoInstances); listView.setAdapter(adapter); listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE); listView.setOnItemClickListener(this); registerForContextMenu(listView); WeakRefHandler mHandler = new WeakRefHandler(this); nsdSearch = new NSDSearch(mHandler); nsdSearch.start(); } @Override protected void onStop() { super.onStop(); stopDiscovery(); } private void stopDiscovery() { if (nsdSearch != null) { nsdSearch.stopDiscovery(); nsdSearch.interrupt(); nsdSearch = null; } } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.autohost_context_menu, menu); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item .getMenuInfo(); switch (item.getItemId()) { case R.id.setActive: setListItemChecked(info.position); break; default: return super.onContextItemSelected(item); } return true; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { super.onBackPressed(); stopDiscovery(); this.overridePendingTransition(R.anim.slideback, R.anim.soutback); return true; } return super.onKeyDown(keyCode, event); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.auto_configuration, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.saveAll) { super.onBackPressed(); if (list.size() > 0) { saveAllHost(); } else { Intent intent = new Intent(AutoConfigurationActivity.this, HostSetActivity.class); intent.putExtra(ADD_MANUAL, true); AutoConfigurationActivity.this.startActivity(intent); } this.overridePendingTransition(R.anim.slide, R.anim.sout); return true; } else if (id == android.R.id.home) { super.onBackPressed(); this.overridePendingTransition(R.anim.slideback, R.anim.soutback); } return super.onOptionsItemSelected(item); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == SHOW_HOST) { if (resultCode == RESULT_OK) { if (data != null) { Host h = (Host) data .getSerializableExtra(HostSetActivity.INSTANCE_NAME); list.set(clicked, h); adapter.notifyDataSetChanged(); } } } } private void setListItemChecked(int pos) { listView.setItemChecked(pos, true); checkedPos = pos; } private void saveAllHost() { HostsDataSource dataSource = HostsDataSource.getInstance(); int checkedPos = listView.getCheckedItemPosition(); for (Host h : list) { int id = dataSource.contains(h); if (id == -1) { dataSource.addHost(h); } else { h.setID(id); } } Host active = list.get(checkedPos); SharedPreferencesUtil.changePreferences(active); Connection.getInstance().setHost(active); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (checkedPos != position) { setListItemChecked(checkedPos); } clicked = position; Intent intent = new Intent(this, HostSetActivity.class); Host hostToSend = list.get(position); intent.putExtra(HostSetActivity.INSTANCE_NAME, hostToSend); intent.putExtra(TO_EDIT, true); startActivityForResult(intent, SHOW_HOST); this.overridePendingTransition(R.anim.slide, R.anim.sout); } private static class WeakRefHandler extends Handler { private final WeakReference<AutoConfigurationActivity> ref; public WeakRefHandler(AutoConfigurationActivity ref) { this.ref = new WeakReference<AutoConfigurationActivity>(ref); } @Override public void handleMessage(Message msg) { AutoConfigurationActivity ac = ref.get(); switch (msg.what) { case NSDSearch.ADD_TO_RESOLVED: NsdServiceInfo parcel = msg.getData() .getParcelable(NSDSearch.RESOLVED); Host h = new Host(); String name = parcel.getServiceName(); name = name.replace("\\", "/").replaceAll("/032", " "); String ip = parcel.getHost().toString(); if (ip != null && ip.length() > 1) { ip = ip.substring(1); } h.setProfileName(name); h.setIp(ip); h.setPort(parcel.getPort()); h.setUsername("xbmc"); ac.list.add(h); if (ac.list.size() > 0) { ac.setListItemChecked(0); } ac.adapter.notifyDataSetChanged(); break; default: break; } super.handleMessage(msg); } } }