Android Open Source - DroidUPnP Main






From Project

Back to project page DroidUPnP.

License

The source code is released under:

GNU General Public License

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

/**
 * Copyright (C) 2013 Aurlien Chabot <aurelien@chabot.fr>
 *// www .  j ava2s .c o m
 * This file is part of DroidUPNP.
 *
 * DroidUPNP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DroidUPNP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with DroidUPNP.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.droidupnp;

import android.app.ActionBar;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.widget.DrawerLayout;

import org.droidupnp.controller.upnp.IUpnpServiceController;
import org.droidupnp.model.upnp.IFactory;
import org.droidupnp.view.ContentDirectoryFragment;
import org.droidupnp.view.RendererFragment;
import org.droidupnp.view.SettingsActivity;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.Observer;

public class Main extends ActionBarActivity
{
  private static final String TAG = "Main";

  // Controller
  public static IUpnpServiceController upnpServiceController = null;
  public static IFactory factory = null;

  private static Menu actionBarMenu = null;

  private DrawerFragment mDrawerFragment;
  private CharSequence mTitle;

  public ContentDirectoryFragment getContentDirectoryFragment()
  {
    Fragment f = getFragmentManager().findFragmentById(R.id.ContentDirectoryFragment);
    if(f != null)
      return (ContentDirectoryFragment) f;
    return null;
  }

  public RendererFragment getRenderer()
  {
    Fragment f = getFragmentManager().findFragmentById(R.id.RendererFragment);
    if(f != null)
      return (RendererFragment) f;
    return null;
  }

  public static void setSearchVisibility(boolean visibility)
  {
    if(actionBarMenu == null)
      return;

    MenuItem item = actionBarMenu.findItem(R.id.action_search);

    if(item != null)
      item.setVisible(visibility);
  }

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

    Log.d(TAG, "onCreated : " + savedInstanceState + factory + upnpServiceController);

    // Use cling factory
    if (factory == null)
      factory = new org.droidupnp.controller.cling.Factory();

    // Upnp service
    if (upnpServiceController == null)
      upnpServiceController = factory.createUpnpServiceController(this);

    // Attach listener
    Fragment contentDirectoryFragment = getFragmentManager().findFragmentById(R.id.ContentDirectoryFragment);
    if (contentDirectoryFragment != null && contentDirectoryFragment instanceof Observer)
      upnpServiceController.addSelectedContentDirectoryObserver((Observer) contentDirectoryFragment);
    else
      Log.w(TAG, "No contentDirectoryFragment yet !");

    Fragment rendererFragment = getFragmentManager().findFragmentById(R.id.RendererFragment);
    if (rendererFragment != null && rendererFragment instanceof Observer)
      upnpServiceController.addSelectedRendererObserver((Observer) rendererFragment);
    else
      Log.w(TAG, "No rendererFragment yet !");

    if(getFragmentManager().findFragmentById(R.id.navigation_drawer) instanceof DrawerFragment)
    {
      mDrawerFragment = (DrawerFragment)
          getFragmentManager().findFragmentById(R.id.navigation_drawer);
      mTitle = getTitle();

      // Set up the drawer.
      mDrawerFragment.setUp(
        R.id.navigation_drawer,
        (DrawerLayout) findViewById(R.id.drawer_layout));
    }
  }

  @Override
  public void onResume()
  {
    Log.v(TAG, "Resume activity");
    upnpServiceController.resume(this);
    super.onResume();
  }

  @Override
  public void onPause()
  {
    Log.v(TAG, "Pause activity");
    upnpServiceController.pause();
    upnpServiceController.getServiceListener().getServiceConnexion().onServiceDisconnected(null);
    super.onPause();
  }

  public void refresh()
  {
    upnpServiceController.getServiceListener().refresh();
    ContentDirectoryFragment cd = getContentDirectoryFragment();
    if(cd!=null)
      cd.refresh();
  }

  public void restoreActionBar() {
    ActionBar actionBar = getActionBar();
    if(actionBar!=null) {
      actionBar.setDisplayShowTitleEnabled(true);
      actionBar.setTitle(mTitle);
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    actionBarMenu = menu;
    restoreActionBar();
    return super.onCreateOptionsMenu(menu);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    // Handle item selection
    switch (item.getItemId())
    {
      case R.id.menu_refresh:
        refresh();
        break;
      case R.id.menu_settings:
        startActivity(new Intent(this, SettingsActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP));
        break;
      case R.id.menu_quit:
        finish();
        break;
      default:
        return super.onOptionsItemSelected(item);
    }
    return super.onOptionsItemSelected(item);
  }

  @Override
  public void onBackPressed()
  {
    ContentDirectoryFragment cd = getContentDirectoryFragment();
    if(cd!=null)
      if (cd.goBack())
        super.onBackPressed();
  }

  private static InetAddress getLocalIpAdressFromIntf(String intfName)
  {
    try
    {
      NetworkInterface intf = NetworkInterface.getByName(intfName);
      if(intf.isUp())
      {
        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)
        {
          InetAddress inetAddress = enumIpAddr.nextElement();
          if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address)
            return inetAddress;
        }
      }
    } catch (Exception e) {
      Log.e(TAG, "Unable to get ip adress for interface " + intfName);
    }
    return null;
  }

  public static InetAddress getLocalIpAddress(Context ctx) throws UnknownHostException
  {
    WifiManager wifiManager = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ipAddress = wifiInfo.getIpAddress();
    if(ipAddress!=0)
      return InetAddress.getByName(String.format("%d.%d.%d.%d",
        (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
        (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff)));

    Log.d(TAG, "No ip adress available throught wifi manager, try to get it manually");

    InetAddress inetAddress;

    inetAddress = getLocalIpAdressFromIntf("wlan0");
    if(inetAddress!=null)
    {
      Log.d(TAG, "Got an ip for interfarce wlan0");
      return inetAddress;
    }

    inetAddress = getLocalIpAdressFromIntf("usb0");
    if(inetAddress!=null)
    {
      Log.d(TAG, "Got an ip for interfarce usb0");
      return inetAddress;
    }

    return InetAddress.getByName("0.0.0.0");
  }
}




Java Source Code List

fi.iki.elonen.NanoHTTPD.java
fi.iki.elonen.ServerRunner.java
fi.iki.elonen.SimpleWebServer.java
org.droidupnp.DrawerFragment.java
org.droidupnp.Main.java
org.droidupnp.controller.cling.ContentDirectoryCommand.java
org.droidupnp.controller.cling.Factory.java
org.droidupnp.controller.cling.RendererCommand.java
org.droidupnp.controller.cling.ServiceController.java
org.droidupnp.controller.cling.ServiceListener.java
org.droidupnp.controller.upnp.IUpnpServiceController.java
org.droidupnp.controller.upnp.UpnpDebugListener.java
org.droidupnp.model.CObservable.java
org.droidupnp.model.cling.CDevice.java
org.droidupnp.model.cling.CRegistryListener.java
org.droidupnp.model.cling.RendererState.java
org.droidupnp.model.cling.TrackMetadata.java
org.droidupnp.model.cling.UpnpRegistry.java
org.droidupnp.model.cling.UpnpServiceController.java
org.droidupnp.model.cling.UpnpService.java
org.droidupnp.model.cling.didl.ClingAudioItem.java
org.droidupnp.model.cling.didl.ClingDIDLContainer.java
org.droidupnp.model.cling.didl.ClingDIDLItem.java
org.droidupnp.model.cling.didl.ClingDIDLObject.java
org.droidupnp.model.cling.didl.ClingDIDLParentContainer.java
org.droidupnp.model.cling.didl.ClingImageItem.java
org.droidupnp.model.cling.didl.ClingVideoItem.java
org.droidupnp.model.cling.localContent.AlbumContainer.java
org.droidupnp.model.cling.localContent.ArtistContainer.java
org.droidupnp.model.cling.localContent.AudioContainer.java
org.droidupnp.model.cling.localContent.CustomContainer.java
org.droidupnp.model.cling.localContent.DynamicContainer.java
org.droidupnp.model.cling.localContent.ImageContainer.java
org.droidupnp.model.cling.localContent.VideoContainer.java
org.droidupnp.model.mediaserver.ContentDirectoryService.java
org.droidupnp.model.mediaserver.MediaServer.java
org.droidupnp.model.upnp.ARendererState.java
org.droidupnp.model.upnp.CallableContentDirectoryFilter.java
org.droidupnp.model.upnp.CallableRendererFilter.java
org.droidupnp.model.upnp.ContentDirectoryDiscovery.java
org.droidupnp.model.upnp.DeviceDiscovery.java
org.droidupnp.model.upnp.DeviceListener.java
org.droidupnp.model.upnp.ICallableFilter.java
org.droidupnp.model.upnp.IContentDirectoryCommand.java
org.droidupnp.model.upnp.IDeviceDiscoveryObserver.java
org.droidupnp.model.upnp.IFactory.java
org.droidupnp.model.upnp.IRegistryListener.java
org.droidupnp.model.upnp.IRendererCommand.java
org.droidupnp.model.upnp.IRendererState.java
org.droidupnp.model.upnp.IServiceListener.java
org.droidupnp.model.upnp.IUpnpDevice.java
org.droidupnp.model.upnp.IUpnpRegistry.java
org.droidupnp.model.upnp.PeeringConnectionManager.java
org.droidupnp.model.upnp.RendererDiscovery.java
org.droidupnp.model.upnp.didl.DIDLDevice.java
org.droidupnp.model.upnp.didl.IDIDLContainer.java
org.droidupnp.model.upnp.didl.IDIDLItem.java
org.droidupnp.model.upnp.didl.IDIDLObject.java
org.droidupnp.model.upnp.didl.IDIDLParentContainer.java
org.droidupnp.view.ContentDirectoryDeviceFragment.java
org.droidupnp.view.ContentDirectoryDialog.java
org.droidupnp.view.ContentDirectoryEnabler.java
org.droidupnp.view.ContentDirectoryFragment.java
org.droidupnp.view.Content.java
org.droidupnp.view.DIDLObjectDisplay.java
org.droidupnp.view.DeviceDisplay.java
org.droidupnp.view.DeviceFragment.java
org.droidupnp.view.DeviceInfoDialog.java
org.droidupnp.view.PlaylistFragment.java
org.droidupnp.view.RendererDeviceFragment.java
org.droidupnp.view.RendererDialog.java
org.droidupnp.view.RendererFragment.java
org.droidupnp.view.ServiceDiscoveryFragment.java
org.droidupnp.view.SettingsActivity.java
org.droidupnp.view.UpnpDeviceListFragment.java