Android Open Source - DroidUPnP Settings Activity






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>
 *// w ww  . ja  va 2s.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.view;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.v4.app.NavUtils;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.Switch;
import android.widget.TextView;

import org.droidupnp.R;

import java.util.ArrayList;
import java.util.List;

import de.psdev.licensesdialog.LicensesDialog;
import de.psdev.licensesdialog.licenses.ApacheSoftwareLicense20;
import de.psdev.licensesdialog.licenses.BSD3ClauseLicense;
import de.psdev.licensesdialog.licenses.GnuLesserGeneralPublicLicense21;
import de.psdev.licensesdialog.model.Notice;
import de.psdev.licensesdialog.model.Notices;

public class SettingsActivity extends PreferenceActivity {

  public static final String CONTENTDIRECTORY_SERVICE = "pref_contentDirectoryService";
  public static final String CONTENTDIRECTORY_NAME = "pref_contentDirectoryService_name";
  public static final String CONTENTDIRECTORY_SHARE = "pref_contentDirectoryService_share";
  public static final String CONTENTDIRECTORY_VIDEO = "pref_contentDirectoryService_video";
  public static final String CONTENTDIRECTORY_AUDIO = "pref_contentDirectoryService_audio";
  public static final String CONTENTDIRECTORY_IMAGE = "pref_contentDirectoryService_image";

  private Toolbar mActionBar;

  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    mActionBar.setTitle(getTitle());
//    mActionBar.setDisplayHomeAsUpEnabled(true);
  }

  @Override
  public void setContentView(int layoutResID)
  {
    ViewGroup contentView = (ViewGroup) LayoutInflater.from(this).inflate(
        R.layout.preference_activity, new LinearLayout(this), false);

    mActionBar = (Toolbar) contentView.findViewById(R.id.action_bar);
    mActionBar.setNavigationOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        finish();
      }
    });

    ViewGroup contentWrapper = (ViewGroup) contentView.findViewById(R.id.content_wrapper);
    LayoutInflater.from(this).inflate(layoutResID, contentWrapper, true);

    getWindow().setContentView(contentView);
  }

  public static String getSettingContentDirectoryName(Context ctx)
  {
    String value = PreferenceManager.getDefaultSharedPreferences(ctx)
      .getString(SettingsActivity.CONTENTDIRECTORY_NAME, "");
    return (value != "") ? value : android.os.Build.MODEL;
  }

  private static final String TAG = "SettingsActivity";

  private List<Header> mHeaders;

  @Override
  protected void onResume()
  {
    super.onResume();

    if (getListAdapter() instanceof MyPrefsHeaderAdapter)
      ((MyPrefsHeaderAdapter) getListAdapter()).resume();
  }

  @Override
  protected void onPause()
  {
    super.onPause();

    if (getListAdapter() instanceof MyPrefsHeaderAdapter)
      ((MyPrefsHeaderAdapter) getListAdapter()).pause();
  }

  @Override
  protected boolean isValidFragment (String fragmentName)
  {
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item)
  {
    switch (item.getItemId()) {
      // Respond to the action bar's Up/Home button
      case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override
  public void onBuildHeaders(List<PreferenceActivity.Header> target)
  {
    loadHeadersFromResource(R.xml.preferences, target);
    mHeaders = target;
  }

  public void setListAdapter(ListAdapter adapter)
  {
    if (mHeaders == null) {
      mHeaders = new ArrayList<>();
      for (int i = 0; i < adapter.getCount(); ++i)
        mHeaders.add((Header) adapter.getItem(i));
    }
    super.setListAdapter(new MyPrefsHeaderAdapter(this, mHeaders));
  }

  public static class ContentDirectorySettingsFragment extends PreferenceFragment
    implements SharedPreferences.OnSharedPreferenceChangeListener
  {
    private ContentDirectoryEnabler enabler;

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
      super.onActivityCreated(savedInstanceState);

      final Activity activity = getActivity();

      PreferenceManager.getDefaultSharedPreferences(activity).registerOnSharedPreferenceChangeListener(this);
      addPreferencesFromResource(R.xml.contentdirectory);

      Switch actionBarSwitch = new Switch(activity);

/*
      if(activity instanceof PreferenceActivity)
      {
        PreferenceActivity preferenceActivity = (PreferenceActivity) activity;
        if(preferenceActivity.onIsHidingHeaders() || !preferenceActivity.onIsMultiPane())
        {
          activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
              ActionBar.DISPLAY_SHOW_CUSTOM);
          activity.getActionBar().setCustomView(actionBarSwitch, new ActionBar.LayoutParams(
            ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT,
            Gravity.CENTER_VERTICAL | Gravity.END));
        }
      }
      */
      enabler = new ContentDirectoryEnabler(getActivity(), actionBarSwitch);
      updateSettings();

      EditTextPreference editTextPref = (EditTextPreference) findPreference(SettingsActivity.CONTENTDIRECTORY_NAME);
      if(editTextPref != null)
        editTextPref.setSummary(getSettingContentDirectoryName(activity));
    }

    public void onResume()
    {
      super.onResume();
      enabler.resume();
      updateSettings();
    }

    public void onPause()
    {
      super.onPause();
      enabler.pause();
    }

    protected void updateSettings()
    {
      boolean available = enabler.isSwitchOn();
      Log.d("ContentDirectorySettingsFragment", "updateSettings " + (available ? " true" : " false"));

      // Enable or not preference field
      for(int i = 0; i < getPreferenceScreen().getPreferenceCount(); ++i)
        getPreferenceScreen().getPreference(i).setEnabled(available);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
    {
      Log.d("ContentDirectorySettingsFragment", "onSharedPreferenceChanged " + key);

      if (key.equals(SettingsActivity.CONTENTDIRECTORY_SERVICE))
      {
        updateSettings();
      }
      else if(key.equals(SettingsActivity.CONTENTDIRECTORY_NAME))
      {
        EditTextPreference editTextPref = (EditTextPreference) findPreference(SettingsActivity.CONTENTDIRECTORY_NAME);
        if(editTextPref != null )
          editTextPref.setSummary(getSettingContentDirectoryName(getActivity()));
      }
    }
  }

  public static class AboutSettingsFragment extends PreferenceFragment
  {
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
      super.onCreate(savedInstanceState);
      addPreferencesFromResource(R.xml.about);

      // Set version
      try {
        Preference pref = findPreference("version");
        pref.setSummary(getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(), 0).versionName);
      } catch (PackageManager.NameNotFoundException e) {
        Log.e(TAG, "exception", e);
      }

      // Dialog for external license
      Preference pref = findPreference("licenses_other");
      pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {
          Notices notices = new Notices();
          notices.addNotice(new Notice(
            "AppCompat", "http://developer.android.com/tools/support-library/",
            "Copyright (C) The Android Open Source Project", new ApacheSoftwareLicense20()));
          notices.addNotice(new Notice(
            "Cling", "http://4thline.org/projects/cling/",
            "Copyright (C) 4th Line GmbH", new GnuLesserGeneralPublicLicense21()));
          notices.addNotice(new Notice(
            "NanoHttpd", "https://github.com/NanoHttpd/nanohttpd",
            "Copyright (C) 2012-2013 by Paul S. Hawke, 2001,2005-2013 by Jarno Elonen, 2010 by Konstantinos Togias", new BSD3ClauseLicense()));
          notices.addNotice(new Notice(
            "ActionBar-PullToRefresh", "https://github.com/chrisbanes/ActionBar-PullToRefresh",
            "Copyright (C) Chris Banes", new ApacheSoftwareLicense20()));
          notices.addNotice(new Notice(
            "LicenseDialog", "http://psdev.de/LicensesDialog/",
            "Copyright (C) Philip Schiffer", new ApacheSoftwareLicense20()));

          LicensesDialog.Builder licensesDialog = new LicensesDialog.Builder(getActivity());
          licensesDialog.setNotices(notices);
          licensesDialog.setTitle(R.string.licenses_other);
          licensesDialog.build().show();
          return false;
        }

      });
    }
  }

  public static class MyPrefsHeaderAdapter extends ArrayAdapter<Header>
  {
    static final int HEADER_TYPE_CATEGORY = 0;
    static final int HEADER_TYPE_NORMAL = 1;
    static final int HEADER_TYPE_SWITCH = 2;

    private LayoutInflater mInflater;
    private ContentDirectoryEnabler mContentDirectoryEnabler;

    public MyPrefsHeaderAdapter(Context context, List<Header> objects)
    {
      super(context, 0, objects);
      mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      mContentDirectoryEnabler = new ContentDirectoryEnabler(context, new Switch(context));
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
      Header header = getItem(position);
      int headerType = getHeaderType(header);
      View view = null;

      switch (headerType)
      {
        case HEADER_TYPE_CATEGORY:
          view = mInflater.inflate(android.R.layout.preference_category, parent, false);
          ((TextView) view.findViewById(android.R.id.title)).setText(header.getTitle(getContext().getResources()));
          break;

        case HEADER_TYPE_SWITCH:
          view = mInflater.inflate(R.layout.preference_header_switch_item, parent, false);
          ((ImageView) view.findViewById(android.R.id.icon)).setImageResource(header.iconRes);
          ((TextView) view.findViewById(android.R.id.title)).setText(header.getTitle(getContext().getResources()));
          ((TextView) view.findViewById(android.R.id.summary)).setText(header.getSummary(getContext().getResources()));

          if(header.id == R.id.contentdirectory_settings)
            mContentDirectoryEnabler = new ContentDirectoryEnabler(getContext(),
              (Switch) view.findViewById(R.id.switchWidget));
          break;

        case HEADER_TYPE_NORMAL:
          view = mInflater.inflate(R.layout.preference_header_item, parent, false);
          ((ImageView) view.findViewById(android.R.id.icon)).setImageResource(header.iconRes);
          ((TextView) view.findViewById(android.R.id.title)).setText(header.getTitle(getContext().getResources()));
          ((TextView) view.findViewById(android.R.id.summary)).setText(header.getSummary(getContext().getResources()));
          break;
      }

      return view;
    }

    public static int getHeaderType(Header header)
    {
      if ((header.fragment == null) && (header.intent == null)) {
        return HEADER_TYPE_CATEGORY;
      } else if (header.id == R.id.contentdirectory_settings) {
        return HEADER_TYPE_SWITCH;
      } else {
        return HEADER_TYPE_NORMAL;
      }
    }

    public void resume() {
      mContentDirectoryEnabler.resume();
    }

    public void pause() {
      mContentDirectoryEnabler.pause();
    }
  }
}




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