Android Open Source - PlayerHater Config






From Project

Back to project page PlayerHater.

License

The source code is released under:

Apache License

If you think the Android project PlayerHater 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 2013 Chris Rhoden, Rebecca Nesson, Public Radio Exchange
 * //from  w ww . ja  va  2 s. c  o  m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package org.prx.playerhater.util;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.prx.playerhater.PlayerHater;
import org.prx.playerhater.PlayerHaterPlugin;
import org.prx.playerhater.R;
import org.prx.playerhater.plugins.PluginCollection;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.content.res.Resources.NotFoundException;
import android.os.Parcel;
import android.os.Parcelable;

public class Config implements Parcelable {
  private static final String EXTRA_CONFIG = "config";
  private static Config sInstance;

  public static void attachToIntent(Intent intent) {
    if (sInstance != null) {
      intent.putExtra(EXTRA_CONFIG, sInstance);
    }
  }

  public static Config fromIntent(Intent intent) {
    return intent.getExtras().getParcelable(Config.EXTRA_CONFIG);
  }

  public static Config getInstance(Context context) {
    if (sInstance == null) {
      sInstance = new Config(context);
    }
    return sInstance;
  }

  public PluginCollection run(Context context, PlayerHater playerHater) {
    PluginCollection collection = new PluginCollection();
    return run(context, playerHater, collection);
  }

  public PluginCollection run(Context context, PlayerHater playerHater,
      PluginCollection collection) {
    collection.writeLock();
    for (Class<? extends PlayerHaterPlugin> pluginKlass : getPlugins()) {
      try {
        PlayerHaterPlugin plugin = pluginKlass.newInstance();
        collection.add(plugin);
      } catch (Exception e) {
        Log.e("Could not instantiate plugin "
            + pluginKlass.getCanonicalName(), e);
      }
    }
    collection.onPlayerHaterLoaded(context, playerHater);
    collection.unWriteLock();
    return collection;
  }

  private final Set<String> mPlugins = new HashSet<String>();

  private Config(Context context) {
    XmlResourceParser parser = context.getResources().getXml(
        R.xml.zzz_ph_config_defaults);
    load(parser, context);
    try {
      ServiceInfo info = context.getPackageManager().getServiceInfo(
          PlayerHater.buildServiceIntent(context).getComponent(),
          PackageManager.GET_META_DATA);
      if (info != null && info.metaData != null) {
        int id = info.metaData.getInt("org.prx.playerhater.Config", 0);
        if (id != 0) {
          parser = context.getResources().getXml(id);
          load(parser, context);
        }
      }
    } catch (NameNotFoundException e) {
      // If this happens, we can just use the default configuration.
    }
  }

  private Set<Class<? extends PlayerHaterPlugin>> getPlugins() {
    return getPlugins(mPlugins);
  }

  @SuppressWarnings("unchecked")
  private Set<Class<? extends PlayerHaterPlugin>> getPlugins(
      Set<String> strings) {
    Set<Class<? extends PlayerHaterPlugin>> plugins = new HashSet<Class<? extends PlayerHaterPlugin>>();
    for (String pluginName : strings) {
      try {
        plugins.add((Class<? extends PlayerHaterPlugin>) Class
            .forName(pluginName));
      } catch (Exception e) {
        Log.e("Can't load plugin " + pluginName, e);
      }
    }
    return plugins;
  }

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeStringArray(getPluginsArray());
  }

  public static final Parcelable.Creator<Config> CREATOR = new Parcelable.Creator<Config>() {

    @Override
    public Config createFromParcel(Parcel in) {
      return new Config(in);
    }

    @Override
    public Config[] newArray(int size) {
      return new Config[size];
    }
  };

  private static final int PLUGIN = 1;
  private static final int INVALID_TAG = -1;

  private Config(Parcel in) {
    setPluginsArray(in.createStringArray());
  }

  private String[] getPluginsArray() {
    return mPlugins.toArray(new String[mPlugins.size() - 1]);
  }

  private void setPluginsArray(String[] plugins) {
    setStringArray(plugins, mPlugins);
  }

  private void setStringArray(String[] stuff, Set<String> in) {
    in.clear();
    Collections.addAll(in, stuff);
  }

  private void load(XmlResourceParser parser, Context context) {
    Resources res = context.getResources();
    try {
      parser.next();
      int eventType = parser.getEventType();
      boolean pluginEnabled = false;
      boolean pluginDisabled = false;
      String pluginName = null;
      int currentTagType = INVALID_TAG;

      while (eventType != XmlResourceParser.END_DOCUMENT) {
        if (eventType == XmlResourceParser.START_TAG) {
          if (parser.getName().equals("plugin")) {
            currentTagType = PLUGIN;
          }
          pluginEnabled = loadBooleanOrResourceBoolean(res, parser,
              "enabled", true);
          pluginDisabled = loadBooleanOrResourceBoolean(res, parser,
              "disabled", false);

          pluginName = parser.getAttributeValue(null, "name");
        } else if (eventType == XmlResourceParser.END_TAG) {
          switch (currentTagType) {
          case PLUGIN:
            if (pluginEnabled && pluginName != null) {
              mPlugins.add(pluginName);
            } else if (pluginDisabled && pluginName != null) {
              mPlugins.remove(pluginName);
            }
            break;
          }
        }
        eventType = parser.next();
      }
    } catch (Exception e) {

    }
  }

  private boolean loadBooleanOrResourceBoolean(Resources res,
      XmlResourceParser parser, String attrName, boolean def) {
    int id;
    boolean result = def;
    try {
      id = parser.getAttributeResourceValue(null, attrName, 0);
      if (id != 0) {
        try {
          result = res.getBoolean(id);
        } catch (NotFoundException e) {
          result = parser.getAttributeBooleanValue(null, attrName,
              def);
        }
      } else {
        result = parser.getAttributeBooleanValue(null, attrName, def);
      }
    } catch (Exception e) {
      return result;
    }
    return result;
  }
}




Java Source Code List

.AbstractPlugin.java
.AudioFocusPlugin.java
.BackgroundedPlugin.java
.BoundPlayerHater.java
.BroadcastReceiver.java
.ClientPlugin.java
.Config.java
.ExpandableNotificationPlugin.java
.HeadphoneButtonGestureHelper.java
.IPlayerHater.java
.LockScreenControlsPlugin.java
.Log.java
.MediaPlayerPool.java
.NotificationPlugin.java
.OnAudioFocusChangedListener.java
.PebblePlugin.java
.PlaybackService.java
.PlayerHaterClient.java
.PlayerHaterListenerPlugin.java
.PlayerHaterListener.java
.PlayerHaterPlugin.java
.PlayerHaterServer.java
.PlayerHaterService.java
.PlayerHater.java
.PlayerStateWatcher.java
.Player.java
.PlaylistParser.java
.PlaylistSupportingPlayer.java
.PluginCollection.java
.Receiver.java
.RemoteControlButtonReceiver.java
.RemoteSong.java
.ServerPlayerHater.java
.ServicePlayerHater.java
.SongHost.java
.SongQueue.java
.Song.java
.Songs.java
.StatelyPlayer.java
.SynchronousPlayer.java
.ThreadsafePlayerHater.java
.ThreadsafeServicePlayerHater.java
.TouchableNotificationPlugin.java
org.prx.playerhater.BroadcastReceiver.java
org.prx.playerhater.PlaybackService.java
org.prx.playerhater.PlayerHaterListener.java
org.prx.playerhater.PlayerHaterPlugin.java
org.prx.playerhater.PlayerHater.java
org.prx.playerhater.Song.java
org.prx.playerhater.broadcast.HeadphoneButtonGestureHelper.java
org.prx.playerhater.broadcast.OnAudioFocusChangedListener.java
org.prx.playerhater.broadcast.Receiver.java
org.prx.playerhater.broadcast.RemoteControlButtonReceiver.java
org.prx.playerhater.ipc.ClientPlugin.java
org.prx.playerhater.ipc.PlayerHaterClient.java
org.prx.playerhater.ipc.PlayerHaterServer.java
org.prx.playerhater.ipc.ServerPlayerHater.java
org.prx.playerhater.mediaplayer.MediaPlayerPool.java
org.prx.playerhater.mediaplayer.Player.java
org.prx.playerhater.mediaplayer.PlaylistSupportingPlayer.java
org.prx.playerhater.mediaplayer.StatelyPlayer.java
org.prx.playerhater.mediaplayer.SynchronousPlayer.java
org.prx.playerhater.plugins.AbstractPlugin.java
org.prx.playerhater.plugins.AudioFocusPlugin.java
org.prx.playerhater.plugins.BackgroundedPlugin.java
org.prx.playerhater.plugins.ExpandableNotificationPlugin.java
org.prx.playerhater.plugins.LockScreenControlsPlugin.java
org.prx.playerhater.plugins.NotificationPlugin.java
org.prx.playerhater.plugins.PebblePlugin.java
org.prx.playerhater.plugins.PlayerHaterListenerPlugin.java
org.prx.playerhater.plugins.PluginCollection.java
org.prx.playerhater.plugins.ScrubbableLockScreenControlsPlugin.java
org.prx.playerhater.plugins.TouchableNotificationPlugin.java
org.prx.playerhater.service.PlayerHaterService.java
org.prx.playerhater.service.PlayerStateWatcher.java
org.prx.playerhater.songs.RemoteSong.java
org.prx.playerhater.songs.SongHost.java
org.prx.playerhater.songs.SongQueue.java
org.prx.playerhater.songs.Songs.java
org.prx.playerhater.util.Config.java
org.prx.playerhater.util.IPlayerHater.java
org.prx.playerhater.util.Log.java
org.prx.playerhater.util.PlaylistParser.java
org.prx.playerhater.wrappers.BoundPlayerHater.java
org.prx.playerhater.wrappers.ServicePlayerHater.java
org.prx.playerhater.wrappers.ThreadsafePlayerHater.java
org.prx.playerhater.wrappers.ThreadsafeServicePlayerHater.java