Example usage for android.content ContextWrapper ContextWrapper

List of usage examples for android.content ContextWrapper ContextWrapper

Introduction

In this page you can find the example usage for android.content ContextWrapper ContextWrapper.

Prototype

public ContextWrapper(Context base) 

Source Link

Usage

From source file:com.andrew.apollo.utils.MusicUtils.java

/**
 * @param context The {@link Context} to use
 * @param callback The {@link ServiceConnection} to use
 * @return The new instance of {@link ServiceToken}
 *//*from   ww w  .j a  va 2s  . c  o m*/
public static ServiceToken bindToService(final Context context, final ServiceConnection callback) {
    Activity realActivity = ((Activity) context).getParent();
    if (realActivity == null) {
        realActivity = (Activity) context;
    }
    final ContextWrapper contextWrapper = new ContextWrapper(realActivity);
    contextWrapper.startService(new Intent(contextWrapper, MusicPlaybackService.class));
    final ServiceBinder binder = new ServiceBinder(callback);
    if (contextWrapper.bindService(new Intent().setClass(contextWrapper, MusicPlaybackService.class), binder,
            0)) {
        mConnectionMap.put(contextWrapper, binder);
        return new ServiceToken(contextWrapper);
    }
    return null;
}

From source file:com.example.mediastock.util.Utilities.java

/**
 * Method to delete a media file within the typeDir directory. The directory is stored into the internal storage.
 *
 * @param type    the directory type: music, video or image
 * @param context the context/*from w  w  w  .  ja  v  a2 s. co m*/
 * @param path    the path of the media file
 * @return true, if the file was deleted, false otherwise
 */
public static boolean deleteSpecificMediaFromInternalStorage(String type, Context context, String path) {
    ContextWrapper cw = new ContextWrapper(context.getApplicationContext());
    File dir = cw.getDir(type + "Dir", Context.MODE_PRIVATE);

    final File[] fileNames = dir.listFiles();

    boolean result = false;
    for (File file : fileNames) {
        if (file.getName().equals(path))
            result = file.delete();
    }

    return result;
}

From source file:org.bienvenidoainternet.app.ViewerActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    File baiDir = new File(Environment.getExternalStorageDirectory().getPath() + "/Bai/");
    if (!baiDir.exists()) {
        baiDir.mkdir();/*  www .j a  va 2s.c o m*/
    }
    if (item.getItemId() == R.id.menu_save_img) {
        BoardItemFile boardItemFile = fileList.get(imagePager.getCurrentItem());
        File to = new File(Environment.getExternalStorageDirectory().getPath() + "/Bai/" + boardItemFile.file);
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
        File directory = cw.getDir("src", Context.MODE_PRIVATE);
        File fileSource = new File(directory, boardItemFile.boardDir + "_" + boardItemFile.file);
        try {
            InputStream in = new FileInputStream(fileSource);
            OutputStream out = new FileOutputStream(to);
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
            Toast.makeText(getApplicationContext(), boardItemFile.file + " guardado.", Toast.LENGTH_LONG)
                    .show();
            MediaScannerConnection.scanFile(this, new String[] { to.getPath() }, new String[] { "image/jpeg" },
                    null);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    if (item.getItemId() == android.R.id.home) {
        onBackPressed();
    }
    return super.onOptionsItemSelected(item);
}

From source file:com.example.mediastock.util.Utilities.java

/**
 * It loads an image from the storage//from  www . j  a v  a  2s.  c  o  m
 * @param context the context
 * @param path    the path of the image
 * @return the file containing the blob image
 */
public static File loadImageFromInternalStorage(Context context, String path) {
    ContextWrapper cw = new ContextWrapper(context.getApplicationContext());
    File imageDir = cw.getDir("imageDir", Context.MODE_PRIVATE);

    final File[] fileNames = imageDir.listFiles();

    File target = null;
    for (File file : fileNames) {
        if (file.getName().equals(path))
            target = file;
    }

    return target;
}

From source file:com.example.mediastock.util.Utilities.java

/**
 * It loads a media file from the storage.
 * @param type    the typeDir directory, where type can be music or video
 * @param context the context// ww w.j av  a2s .  c  o m
 * @param path    the path to the media file
 * @return a FileInputStream object that contains the media file
 */
public static FileInputStream loadMediaFromInternalStorage(String type, Context context, final String path) {
    ContextWrapper cw = new ContextWrapper(context.getApplicationContext());
    File dir = cw.getDir(type + "Dir", Context.MODE_PRIVATE);

    final File[] fileNames = dir.listFiles();

    File target = null;
    for (File file : fileNames) {
        if (file.getName().equals(path))
            target = file;
    }

    FileInputStream fis = null;
    try {

        fis = new FileInputStream(target);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    return fis;
}

From source file:com.example.mediastock.util.Utilities.java

/**
 * Returns the file containing the blob//  ww  w.j av a 2s. c o m
 *
 * @param type    the typeDir directory, where type can be music or video
 * @param context the context
 * @param path    the path of the file
 * @return the file containing the blob
 */
public static File getFile(String type, Context context, final String path) {
    ContextWrapper cw = new ContextWrapper(context.getApplicationContext());
    File dir = cw.getDir(type + "Dir", Context.MODE_PRIVATE);

    final File[] fileNames = dir.listFiles();

    File target = null;
    for (File file : fileNames) {
        if (file.getName().equals(path))
            target = file;
    }

    return target;
}

From source file:layout.FragmentImage.java

private void downloadFile() {
    downloadBar = ((ViewerActivity) getActivity()).barDownload;
    downloadBar.setVisibility(View.VISIBLE);
    ContextWrapper cw = new ContextWrapper(getContext());
    File directory = cw.getDir("src", Context.MODE_PRIVATE);
    final File filePath = new File(directory, boardItemFile.boardDir + "_" + boardItemFile.file);
    if (filePath.exists()) {
        downloadBar.setVisibility(View.GONE);
        if (boardItemFile.file.endsWith(".gif")) {
            try {
                GifDrawable gifFromFile = new GifDrawable(filePath);
                gifView.setImageDrawable(gifFromFile);
                gifView.setVisibility(View.VISIBLE);
                imageView.setVisibility(View.GONE);
            } catch (Exception e) {
                e.printStackTrace();/* ww  w  . j a v  a  2s . c  o  m*/
            }
        } else {
            imageView.setImage(ImageSource.uri(filePath.toURI().getPath()));
            imageView.setVisibility(View.VISIBLE);
            gifView.setVisibility(View.GONE);
        }
    }
    Ion.with(getContext()).load(boardItemFile.fileURL).progressBar(downloadBar).asInputStream()
            .setCallback(new FutureCallback<InputStream>() {
                @Override
                public void onCompleted(Exception e, InputStream result) {
                    downloadBar.setVisibility(View.GONE);
                    if (e != null) {
                        e.printStackTrace();
                    } else {
                        FileOutputStream fout;
                        try {
                            fout = new FileOutputStream(filePath);
                            final byte data[] = new byte[1024];
                            int count;
                            while ((count = result.read(data, 0, 1024)) != -1) {
                                fout.write(data, 0, count);
                            }
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                        if (boardItemFile.file.endsWith(".gif")) {
                            try {
                                GifDrawable gifFromFile = new GifDrawable(filePath);
                                gifView.setImageDrawable(gifFromFile);
                                gifView.setVisibility(View.VISIBLE);
                                imageView.setVisibility(View.GONE);
                            } catch (Exception e2) {
                                e2.printStackTrace();
                            }
                        } else {
                            imageView.setImage(ImageSource.uri(filePath.toURI().getPath()));
                            gifView.setVisibility(View.GONE);
                            imageView.setVisibility(View.VISIBLE);
                        }
                    }
                }
            });
}

From source file:br.com.viniciuscr.notification2android.mediaPlayer.MusicUtils.java

public static ServiceToken bindToService(Activity context, ServiceConnection callback) {
    Activity realActivity = context.getParent();
    if (realActivity == null) {
        realActivity = context;//from w  ww.j  a v  a 2 s  .  c  o m
    }
    ContextWrapper cw = new ContextWrapper(realActivity);
    cw.startService(new Intent(cw, MediaPlaybackService.class));
    ServiceBinder sb = new ServiceBinder(callback);
    if (cw.bindService((new Intent()).setClass(cw, MediaPlaybackService.class), sb, 0)) {
        sConnectionMap.put(cw, sb);
        return new ServiceToken(cw);
    }
    Log.e("Music", "Failed to bind to service");
    return null;
}

From source file:com.example.mediastock.util.Utilities.java

/**
 * Method to delete all the files within the directory typeDir, where type can be music, image or video
 *
 * @param type    the directory type: music, video or image
 * @param context the context//from   ww w  . j ava 2  s  .com
 */
public static void deleteAllMediaFromInternalStorage(String type, Context context) {
    ContextWrapper cw = new ContextWrapper(context.getApplicationContext());
    File dir = cw.getDir(type + "Dir", Context.MODE_PRIVATE);

    final File[] fileNames = dir.listFiles();

    if (fileNames.length > 0) {
        for (File file : fileNames)
            file.delete();
    }
}