Android Open Source - voicesmith Utils






From Project

Back to project page voicesmith.

License

The source code is released under:

GNU General Public License

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

/*
 * Voicesmith <http://voicesmith.jurihock.de/>
 *//w w  w  .  j  a va  2s .  c  o  m
 * Copyright (c) 2011-2014 Juergen Hock
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.jurihock.voicesmith;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.os.SystemClock;
import android.util.Log;
import android.widget.Toast;

public final class Utils
{
  private static final String        NATIVELIB_NAME  = "Voicesmith";
  private static final String        LOGCAT_TAG    = "Voicesmith";
  private static final int        TOAST_LENGTH  = Toast.LENGTH_LONG;

  private final Context          context;
  private final Preferences        preferences;

  /**
   * Stopwatch timestamps.
   * */
  private static final Map<String, Long>  tics      = new HashMap<String, Long>();

  public Utils(Context context)
  {
    this.context = context;
    this.preferences = new Preferences(context);
  }

  /**
   * Loads the native library.
   * */
  public static void loadNativeLibrary()
  {
    try
    {
      System.loadLibrary(NATIVELIB_NAME);
    }
    catch (UnsatisfiedLinkError exception)
    {
            Log.d(LOGCAT_TAG, String.format(
                    "Native library %s could not be loaded!",
                    NATIVELIB_NAME));
    }
  }

    public String getVersionString(int formatResId)
    {
        return getVersionString(context.getString(formatResId));
    }

    public String getVersionString(String format)
    {
        try
        {
            PackageInfo info = context.getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);

            return String.format(
                    format,
                    info.versionName,
                    info.versionCode);
        }
        catch (PackageManager.NameNotFoundException exception)
        {
            new Utils(context).log(exception);
        }

        return null;
    }

  /**
   * Checks if a local service is just running.
   * */
  public boolean isServiceRunning(Class<?> serviceClass)
  {
    ActivityManager manager = (ActivityManager)
      context.getSystemService(Context.ACTIVITY_SERVICE);

    List<RunningServiceInfo> services =
      manager.getRunningServices(Integer.MAX_VALUE);

    for (RunningServiceInfo service : services)
    {
      if (service.service.getClassName().equals(
        serviceClass.getName()))
      {
        return true;
      }
    }

    return false;
  }

  /**
   * Tries to mount the external storage, makes necessary dirs and finally
   * returns a File instance for assigned file path.
   * */
  public File mountFile(String path) throws IOException
  {
    if (!Environment.MEDIA_MOUNTED.equals(
      Environment.getExternalStorageState()))
    {
      throw new IOException("Unable to mount external storage!");
    }

    File file = new File(
      Environment.getExternalStorageDirectory(),
      path);

    File dir = file.getParentFile();
    if (!dir.exists() && !dir.mkdirs())
    {
      throw new IOException(String.format(
        "Unable to make directory '%s'!",
        dir.getAbsolutePath()));
    }

    return file;
  }

  public void postNotification(int iconID, String tickerText, String contentTitle, String contentText, Class<?> activityClass)
  {
    NotificationManager service = (NotificationManager)
      context.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = new Notification(
      iconID, tickerText, System.currentTimeMillis());
    notification.flags |= Notification.FLAG_AUTO_CANCEL
      | Notification.FLAG_NO_CLEAR;

    Intent intent = new Intent(context, activityClass);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent
      .getActivity(context, 0, intent, 0);

    notification.setLatestEventInfo(context,
      contentTitle, contentText, pendingIntent);

    service.cancel(0);
    service.notify(0, notification);
  }

  public void cancelAllNotifications()
  {
    NotificationManager service = (NotificationManager)
      context.getSystemService(Context.NOTIFICATION_SERVICE);

    service.cancelAll();
  }

  /**
   * Writes a LogCat log entry.
   * */
  public void log(String message)
  {
    if (preferences.isLoggingOn())
    {
      Log.d(LOGCAT_TAG, message);
    }
  }

  /**
   * Writes a formatted LogCat log entry.
   * */
  public void log(String message, Object... args)
  {
    log(String.format(message, args));
  }

  /**
   * Writes a LogCat log entry.
   * */
  public void log(Throwable exception)
  {
    log("[EXCEPTION] " + Log.getStackTraceString(exception));
  }

  /**
   * Shows a Toast message.
   * */
  public void toast(String message)
  {
    Toast.makeText(context, message, TOAST_LENGTH).show();
  }

  /**
   * Shows a formatted Toast message.
   * */
  public void toast(String message, Object... args)
  {
    toast(String.format(message, args));
  }

  /**
   * Writes a LogCat log entry if condition is FALSE.
   * */
  public void assertTrue(boolean condition, String message)
  {
    if (!condition) log("[ASSERT] " + message);
  }

  /**
   * Writes a formatted LogCat log entry if condition is FALSE.
   * */
  public void assertTrue(boolean condition, String message, Object... args)
  {
    assertTrue(condition, String.format(message, args));
  }

  /**
   * Starts a stopwatch.
   * */
  public synchronized void tic(String tag)
  {
    if (tics.containsKey(tag))
    {
      tics.remove(tag);
    }

    long tic = SystemClock.elapsedRealtime(); // ms
    // long tic = System.nanoTime(); // ns

    tics.put(tag, tic);
  }

  /**
   * Stops a stopwatch and prints out the time difference.
   * */
  public synchronized void toc(String tag)
  {
    long toc = SystemClock.elapsedRealtime(); // ms
    // long toc = System.nanoTime(); // ns

    if (tics.containsKey(tag))
    {
      long tic = tics.remove(tag);

      log("%s: %d ms", tag, (toc - tic)); // ms
      // log("%s: %f ms", tag, (toc - tic) / 1000D); // ns/1000
    }
  }
}




Java Source Code List

de.jurihock.voicesmith.AAF.java
de.jurihock.voicesmith.Application.java
de.jurihock.voicesmith.ChangeLog.java
de.jurihock.voicesmith.DAFX.java
de.jurihock.voicesmith.Disposable.java
de.jurihock.voicesmith.FrameType.java
de.jurihock.voicesmith.Preferences.java
de.jurihock.voicesmith.Utils.java
de.jurihock.voicesmith.activities.AafActivity.java
de.jurihock.voicesmith.activities.AboutActivity.java
de.jurihock.voicesmith.activities.AboutApplicationActivity.java
de.jurihock.voicesmith.activities.AboutLicenseActivity.java
de.jurihock.voicesmith.activities.AudioServiceActivity.java
de.jurihock.voicesmith.activities.ContributionActivity.java
de.jurihock.voicesmith.activities.DafxActivity.java
de.jurihock.voicesmith.activities.HomeActivity.java
de.jurihock.voicesmith.activities.PreferenceActivity.java
de.jurihock.voicesmith.activities.SupportActivity.java
de.jurihock.voicesmith.audio.AudioDeviceManager.java
de.jurihock.voicesmith.audio.HeadsetManagerListener.java
de.jurihock.voicesmith.audio.HeadsetManager.java
de.jurihock.voicesmith.audio.HeadsetMode.java
de.jurihock.voicesmith.dsp.KissFFT.java
de.jurihock.voicesmith.dsp.LuenbergerObserver.java
de.jurihock.voicesmith.dsp.Math.java
de.jurihock.voicesmith.dsp.SchmittTrigger.java
de.jurihock.voicesmith.dsp.Window.java
de.jurihock.voicesmith.dsp.processors.AmplifyProcessor.java
de.jurihock.voicesmith.dsp.processors.DenoiseProcessor.java
de.jurihock.voicesmith.dsp.processors.DetuneProcessor.java
de.jurihock.voicesmith.dsp.processors.HoarsenessProcessor.java
de.jurihock.voicesmith.dsp.processors.NativeResampleProcessor.java
de.jurihock.voicesmith.dsp.processors.NativeTimescaleProcessor.java
de.jurihock.voicesmith.dsp.processors.OffsetProcessor.java
de.jurihock.voicesmith.dsp.processors.ResampleProcessor.java
de.jurihock.voicesmith.dsp.processors.RobotizeProcessor.java
de.jurihock.voicesmith.dsp.processors.SeparationProcessor.java
de.jurihock.voicesmith.dsp.processors.TimescaleProcessor.java
de.jurihock.voicesmith.dsp.processors.VadProcessor.java
de.jurihock.voicesmith.dsp.stft.StftPostprocessor.java
de.jurihock.voicesmith.dsp.stft.StftPreprocessor.java
de.jurihock.voicesmith.io.AudioDevice.java
de.jurihock.voicesmith.io.dummy.DummyInDevice.java
de.jurihock.voicesmith.io.dummy.DummyOutDevice.java
de.jurihock.voicesmith.io.file.FileDevice.java
de.jurihock.voicesmith.io.file.FileInDevice.java
de.jurihock.voicesmith.io.file.FileOutDevice.java
de.jurihock.voicesmith.io.oscillators.CosineWave.java
de.jurihock.voicesmith.io.oscillators.PhaseAccumulator.java
de.jurihock.voicesmith.io.pcm.DelayedPcmInDevice.java
de.jurihock.voicesmith.io.pcm.PcmDevice.java
de.jurihock.voicesmith.io.pcm.PcmInDevice.java
de.jurihock.voicesmith.io.pcm.PcmOutDevice.java
de.jurihock.voicesmith.services.AafService.java
de.jurihock.voicesmith.services.AudioService.java
de.jurihock.voicesmith.services.DafxService.java
de.jurihock.voicesmith.services.ServiceBinder.java
de.jurihock.voicesmith.services.ServiceFailureReason.java
de.jurihock.voicesmith.services.ServiceListener.java
de.jurihock.voicesmith.threads.AudioThread.java
de.jurihock.voicesmith.threads.DelayThread.java
de.jurihock.voicesmith.threads.DetuneThread.java
de.jurihock.voicesmith.threads.HoarsenessThread.java
de.jurihock.voicesmith.threads.LowDelayThread.java
de.jurihock.voicesmith.threads.RobotizeThread.java
de.jurihock.voicesmith.threads.TransposeThread.java
de.jurihock.voicesmith.voicebank.Record.java
de.jurihock.voicesmith.voicebank.RecordsSerializer.java
de.jurihock.voicesmith.voicebank.Records.java
de.jurihock.voicesmith.widgets.AafPicker.java
de.jurihock.voicesmith.widgets.ColoredToggleButton.java
de.jurihock.voicesmith.widgets.DafxPicker.java
de.jurihock.voicesmith.widgets.DelayPicker.java
de.jurihock.voicesmith.widgets.IntervalPicker.java
de.jurihock.voicesmith.widgets.ListPreference.java
de.jurihock.voicesmith.widgets.SeekBarPreference.java