Android Open Source - Android-Apps Auto Configuration 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 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);
        }
    }

}




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