Android Open Source - DVBViewerController Device Selection Activity






From Project

Back to project page DVBViewerController.

License

The source code is released under:

Apache License

If you think the Android project DVBViewerController 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 de.bennir.DVBViewerController;
/*from  ww  w .  ja v a2s . co m*/
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.*;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import de.bennir.DVBViewerController.service.DVBService;
import de.bennir.DVBViewerController.util.ThreadExecutor;
import de.keyboardsurfer.android.widget.crouton.Crouton;
import de.keyboardsurfer.android.widget.crouton.Style;

import javax.jmdns.JmDNS;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceInfo;
import javax.jmdns.ServiceListener;
import java.net.InetAddress;
import java.util.LinkedList;

public class DeviceSelectionActivity extends ListActivity implements ServiceListener {
    private final static String TAG = DeviceSelectionActivity.class.toString();
    private final static String CTRL_TYPE = "_dvbctrl._tcp.local.";
    private final static String HOSTNAME = "DVBController";
    private final static int DELAY = 500;
    private static JmDNS zeroConf = null;
    private WifiManager.MulticastLock mLock = null;
    private Handler resultsUpdated = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.obj != null) {
                boolean result = adapter.notifyFound((String) msg.obj);

                if (result) {
                    adapter.notifyDataSetChanged();
                }
            }
        }
    };
    private Typeface robotoThin;
    private Typeface robotoLight;
    private Typeface robotoCondensed;
    private ServiceAdapter adapter;

    void startProbe() throws Exception {
        if (zeroConf != null)
            stopProbe();

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.known.clear();
                adapter.notifyDataSetChanged();
            }
        });

        WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiinfo = wifi.getConnectionInfo();
        int intaddr = wifiinfo.getIpAddress();

        if (intaddr != 0) {
            byte[] byteaddr = new byte[]{(byte) (intaddr & 0xff), (byte) (intaddr >> 8 & 0xff),
                    (byte) (intaddr >> 16 & 0xff), (byte) (intaddr >> 24 & 0xff)};
            InetAddress addr = InetAddress.getByAddress(byteaddr);

            Log.d(TAG, String.format("found intaddr=%d, addr=%s", intaddr, addr.toString()));

            mLock = wifi.createMulticastLock("DVBController Lock");
            mLock.setReferenceCounted(true);
            mLock.acquire();

            zeroConf = JmDNS.create(addr, HOSTNAME);
            zeroConf.addServiceListener(CTRL_TYPE, DeviceSelectionActivity.this);
        } else {
            // Check Wifi State
            Crouton.makeText(this, R.string.wifistate, Style.ALERT).show();
        }
    }

    void stopProbe() {
        zeroConf.removeServiceListener(CTRL_TYPE, DeviceSelectionActivity.this);

        ThreadExecutor.runTask(new Runnable() {
            @Override
            public void run() {
                try {
                    zeroConf.close();
                    zeroConf = null;
                    mLock.release();
                } catch (Exception e) {
                    Log.d(TAG, String.format("ZeroConf Error: %s", e.getMessage()));
                }
            }
        });
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_deviceselection);

        robotoThin = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Thin.ttf");
        robotoLight = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Light.ttf");
        robotoCondensed = Typeface.createFromAsset(getAssets(), "fonts/RobotoCondensed-Bold.ttf");

        ((TextView) findViewById(R.id.select_device)).setTypeface(robotoLight);

        adapter = new ServiceAdapter(this);
        ListView list = getListView();
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final ServiceInfo serviceInfo = (ServiceInfo) adapter.getItem(position);

                String title = serviceInfo.getPropertyString("CtlN");
                if (title == null)
                    title = serviceInfo.getName();
                final String server = serviceInfo.getHostAddresses()[0];
                final String port = String.valueOf(serviceInfo.getPort());

                Intent mIntent = new Intent(getApplicationContext(), DVBViewerControllerActivity.class);
                mIntent.putExtra(DVBService.DVBHOST_KEY, title);
                mIntent.putExtra(DVBService.DVBIP_KEY, server);
                mIntent.putExtra(DVBService.DVBPORT_KEY, port);

                startActivity(mIntent);

                try {
                    DeviceSelectionActivity.this.stopProbe();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                DeviceSelectionActivity.this.finish();
                overridePendingTransition(R.anim.slide_right, R.anim.nothing);
            }
        });

        ThreadExecutor.runTask(new Runnable() {
            @Override
            public void run() {
                try {
                    startProbe();
                } catch (Exception e) {
                    Log.d(TAG, String.format("onCreate Error: %s", e.getMessage()));
                }
            }
        });

        TextView btnSkip = (TextView) findViewById(R.id.button_skip);
        btnSkip.setTypeface(robotoLight);
        btnSkip.setOnClickListener(new

                                           View.OnClickListener() {
                                               @Override
                                               public void onClick(View view) {
                                                   String title = DVBService.DEMO_DEVICE;
                                                   String server = "127.0.0.1";
                                                   String port = "8000";

                                                   Intent mIntent = new Intent(DeviceSelectionActivity.this, DVBViewerControllerActivity.class);
                                                   mIntent.putExtra(DVBService.DVBHOST_KEY, title);
                                                   mIntent.putExtra(DVBService.DVBIP_KEY, server);
                                                   mIntent.putExtra(DVBService.DVBPORT_KEY, port);

                                                   startActivity(mIntent);

                                                   try {
                                                       DeviceSelectionActivity.this.stopProbe();
                                                   } catch (Exception e) {
                                                       e.printStackTrace();
                                                   }

                                                   DeviceSelectionActivity.this.finish();
                                                   overridePendingTransition(R.anim.slide_right, R.anim.nothing);
                                               }
                                           });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 0, 0, "Refresh")
                .setIcon(R.drawable.ic_action_refresh)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == 0) {
            adapter.known.clear();
            adapter.notifyDataSetChanged();
            ThreadExecutor.runTask(new Runnable() {
                @Override
                public void run() {
                    try {
                        startProbe();
                    } catch (Exception e) {
                        Log.d(TAG, String.format("onCreate Error: %s", e.getMessage()));
                    }
                }
            });
        }
        return false;
    }

    @Override
    public void serviceAdded(ServiceEvent serviceEvent) {
        Log.w(TAG, String.format("serviceAdded(event=\n%s\n)", serviceEvent.toString()));
        final String name = serviceEvent.getName();

        resultsUpdated.sendMessageDelayed(Message.obtain(resultsUpdated, -1, name), DELAY);
    }

    @Override
    public void serviceRemoved(ServiceEvent serviceEvent) {
        Log.w(TAG, String.format("serviceRemoved(event=\n%s\n)", serviceEvent.toString()));
    }

    @Override
    public void serviceResolved(ServiceEvent serviceEvent) {
        Log.w(TAG, String.format("serviceResolved(event=\n%s\n)", serviceEvent.toString()));
    }

    public class ServiceAdapter extends BaseAdapter {
        LayoutInflater inflater;
        LinkedList<ServiceInfo> known = new LinkedList<ServiceInfo>();

        public ServiceAdapter(Context context) {
            this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public boolean notifyFound(String dvbctrl) {
            boolean result = false;

            try {
                Log.d(TAG, String.format("DNS Name: %s", dvbctrl));
                ServiceInfo serviceInfo = zeroConf.getServiceInfo(CTRL_TYPE, dvbctrl);

                if (serviceInfo == null)
                    return result;

                String dvbctrlName = serviceInfo.getPropertyString("CtlN");
                if (dvbctrlName == null)
                    dvbctrlName = serviceInfo.getName();

                for (ServiceInfo service : known) {
                    String knownName = service.getPropertyString("CtlN");
                    if (knownName == null)
                        knownName = service.getName();
                    if (dvbctrlName.equalsIgnoreCase(knownName)) {
                        Log.w(TAG, "Already have DatabaseId loaded = " + dvbctrlName);
                        return result;
                    }
                }

                if (!known.contains(serviceInfo)) {
                    known.add(serviceInfo);
                    result = true;
                }
            } catch (Exception e) {
                Log.d(TAG, String.format("Problem getting ZeroConf information %s", e.getMessage()));
            }

            return result;
        }

        @Override
        public int getCount() {
            return known.size();
        }

        @Override
        public Object getItem(int position) {
            return known.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null)
                convertView = inflater.inflate(R.layout.service_list_item, parent, false);

            ((TextView) convertView.findViewById(R.id.service_name)).setTypeface(robotoCondensed);
            ((TextView) convertView.findViewById(R.id.service_ip)).setTypeface(robotoLight);
            ((TextView) convertView.findViewById(R.id.service_port)).setTypeface(robotoLight);

            try {
                final ServiceInfo serviceInfo = (ServiceInfo) this.getItem(position);

                String title = serviceInfo.getPropertyString("CtlN");
                if (title == null)
                    title = serviceInfo.getName();
                final String server = serviceInfo.getHostAddresses()[0];
                final String port = String.valueOf(serviceInfo.getPort());

                Log.d(TAG, String.format("ZeroConf Server: %s", serviceInfo.getServer()));
                Log.d(TAG, String.format("ZeroConf Port: %s", serviceInfo.getPort()));
                Log.d(TAG, String.format("ZeroConf Title: %s", title));

                ((TextView) convertView.findViewById(R.id.service_name)).setText(title);
                ((TextView) convertView.findViewById(R.id.service_ip)).setText(server);
                ((TextView) convertView.findViewById(R.id.service_port)).setText(port);
            } catch (Exception e) {
                Log.d(TAG, String.format("Problem getting ZeroConf information %s", e.getMessage()));
                ((TextView) convertView.findViewById(R.id.service_name)).setText("Unknown");
                ((TextView) convertView.findViewById(R.id.service_ip)).setText("Unknown");
                ((TextView) convertView.findViewById(R.id.service_port)).setText("Unknown");

            }


            return convertView;
        }
    }
}




Java Source Code List

com.android.datetimepicker.AccessibleLinearLayout.java
com.android.datetimepicker.AccessibleTextView.java
com.android.datetimepicker.Utils.java
com.android.datetimepicker.date.AccessibleDateAnimator.java
com.android.datetimepicker.date.DatePickerController.java
com.android.datetimepicker.date.DatePickerDialog.java
com.android.datetimepicker.date.DayPickerView.java
com.android.datetimepicker.date.SimpleMonthAdapter.java
com.android.datetimepicker.date.SimpleMonthView.java
com.android.datetimepicker.date.TextViewWithCircularIndicator.java
com.android.datetimepicker.date.YearPickerView.java
com.android.datetimepicker.time.AmPmCirclesView.java
com.android.datetimepicker.time.CircleView.java
com.android.datetimepicker.time.RadialPickerLayout.java
com.android.datetimepicker.time.RadialSelectorView.java
com.android.datetimepicker.time.RadialTextsView.java
com.android.datetimepicker.time.TimePickerDialog.java
com.googlecode.eyesfree.utils.TouchExplorationHelper.java
de.bennir.DVBViewerController.ChannelFragment.java
de.bennir.DVBViewerController.ChannelGroupFragment.java
de.bennir.DVBViewerController.DVBViewerControllerActivity.java
de.bennir.DVBViewerController.DeviceSelectionActivity.java
de.bennir.DVBViewerController.EPGFragment.java
de.bennir.DVBViewerController.RemoteFragment.java
de.bennir.DVBViewerController.SettingsFragment.java
de.bennir.DVBViewerController.TimerFragment.java
de.bennir.DVBViewerController.TimerWizardActivity.java
de.bennir.DVBViewerController.TimerWizardModel.java
de.bennir.DVBViewerController.channels.ChanGroupAdapter.java
de.bennir.DVBViewerController.channels.DVBChannelAdapter.java
de.bennir.DVBViewerController.channels.DVBChannel.java
de.bennir.DVBViewerController.epg.EPGInfoAdapter.java
de.bennir.DVBViewerController.epg.EPGInfo.java
de.bennir.DVBViewerController.service.DVBServer.java
de.bennir.DVBViewerController.service.DVBService.java
de.bennir.DVBViewerController.timers.DVBTimer.java
de.bennir.DVBViewerController.timers.TimerAdapter.java
de.bennir.DVBViewerController.util.DateUtils.java
de.bennir.DVBViewerController.util.ThreadExecutor.java
de.bennir.DVBViewerController.view.QuickReturnListView.java
de.bennir.DVBViewerController.wizard.model.AbstractWizardModel.java
de.bennir.DVBViewerController.wizard.model.BranchPage.java
de.bennir.DVBViewerController.wizard.model.ModelCallbacks.java
de.bennir.DVBViewerController.wizard.model.MultipleFixedChoicePage.java
de.bennir.DVBViewerController.wizard.model.PageList.java
de.bennir.DVBViewerController.wizard.model.PageTreeNode.java
de.bennir.DVBViewerController.wizard.model.Page.java
de.bennir.DVBViewerController.wizard.model.ReviewItem.java
de.bennir.DVBViewerController.wizard.model.SingleFixedChoicePage.java
de.bennir.DVBViewerController.wizard.model.TimerDatePage.java
de.bennir.DVBViewerController.wizard.model.TimerInfoPage.java
de.bennir.DVBViewerController.wizard.ui.MultipleChoiceFragment.java
de.bennir.DVBViewerController.wizard.ui.PageFragmentCallbacks.java
de.bennir.DVBViewerController.wizard.ui.ReviewFragment.java
de.bennir.DVBViewerController.wizard.ui.SingleChoiceFragment.java
de.bennir.DVBViewerController.wizard.ui.StepPagerStrip.java
de.bennir.DVBViewerController.wizard.ui.TimerDateFragment.java
de.bennir.DVBViewerController.wizard.ui.TimerInfoFragment.java