Android Open Source - sdl_tester_android Put File Dialog

From Project

Back to project page sdl_tester_android.


The source code is released under:

Copyright (c) 2014, Ford Motor Company All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are m...

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

package com.livio.sdltester.dialogs;
/*  w  w w .  j av  a  2s . c  o  m*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.livio.sdl.SdlImageItem;
import com.livio.sdl.SdlImageItem.SdlImageItemComparator;
import com.livio.sdl.SdlRequestFactory;
import com.livio.sdl.dialogs.BaseAlertDialog;
import com.livio.sdl.dialogs.BaseOkCancelDialog;
import com.livio.sdl.dialogs.ImageListDialog;
import com.livio.sdl.enums.SdlCommand;
import com.livio.sdl.utils.SdlUtils;
import com.livio.sdltester.R;
import com.smartdevicelink.proxy.RPCMessage;
import com.smartdevicelink.proxy.RPCRequest;

public class PutFileDialog extends BaseOkCancelDialog {

  private static final SdlCommand SYNC_COMMAND = SdlCommand.PUT_FILE;
  private static final String DIALOG_TITLE = SYNC_COMMAND.toString();
  private ImageButton ib_putFile_selectAnImage;
  private EditText et_putFile_imageName;
  private CheckBox cb_putFile_isPersistent;
  private CheckBox cb_putFile_addAll;
  private SdlImageItem selectedImage = null;
  private List<SdlImageItem> availableImages;
  private HashMap<SdlImageItem, byte[]> bitmapRawByteMap;
  public PutFileDialog(Context context, List<SdlImageItem> availableImages) {
    super(context, DIALOG_TITLE, R.layout.put_file);
    this.availableImages = availableImages;
    Collections.sort(this.availableImages, new SdlImageItemComparator());
   * Preemptively processes all available images in the background so the calculations
   * don't have to be done when the user makes a selection.  By the time the user
   * clicks on something, the calculations should be complete.
  private void startImageProcessing(){
    final int size = availableImages.size();
    bitmapRawByteMap = new HashMap<SdlImageItem, byte[]>(size);
    // loop through all images and start a thread to process them
    for(final SdlImageItem item : availableImages){
      new Thread(new Runnable() {
        public void run() {
          byte[] bitmapData = SdlUtils.bitmapToByteArray(item.getBitmap(), item.getImageType());
          addDataToMap(item, bitmapData);
   * Allows synchronized editting of the raw byte hash map.  HashMaps are not thread safe,
   * so we must protect the data structure through a synchronized method.
   * @param key The key to add
   * @param value The value to add
  private synchronized void addDataToMap(SdlImageItem key, byte[] value){
    bitmapRawByteMap.put(key, value);
  private void setupViews(){
    ib_putFile_selectAnImage.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        BaseAlertDialog selectImageDialog = new ImageListDialog(context, availableImages);
        selectImageDialog.setListener(new BaseAlertDialog.Listener() {
          public void onResult(Object resultData) {
            selectedImage = (SdlImageItem) resultData;

  private void onItemSelected(SdlImageItem item){
  protected void findViews(View parent) {
    et_putFile_imageName = (EditText) parent.findViewById(;
    cb_putFile_isPersistent = (CheckBox) parent.findViewById(;
    ib_putFile_selectAnImage = (ImageButton) parent.findViewById(;
    cb_putFile_addAll = (CheckBox) parent.findViewById(;
    cb_putFile_addAll.setOnCheckedChangeListener(new OnCheckedChangeListener() {
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  private void enableViews(boolean enable){
    int visibility = (enable) ? View.VISIBLE : View.GONE;
  //dialog button click listeners
  private final DialogInterface.OnClickListener okButtonListener = new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
      List<RPCMessage> messages = new ArrayList<RPCMessage>(availableImages.size());
      boolean persistentFile = cb_putFile_isPersistent.isChecked();
        Set<SdlImageItem> keySet = bitmapRawByteMap.keySet();
        Iterator<SdlImageItem> iterator = keySet.iterator();
          SdlImageItem item =;
          RPCRequest result = SdlRequestFactory.putFile(item.getImageName(), item.getImageType(), persistentFile, bitmapRawByteMap.get(item));
      else if(selectedImage != null){
        String name = et_putFile_imageName.getText().toString();
        if(name.length() > 0){
          RPCRequest result = SdlRequestFactory.putFile(name, selectedImage.getImageType(), persistentFile, bitmapRawByteMap.get(selectedImage));

          Toast.makeText(context, "Must enter a name for the image.", Toast.LENGTH_LONG).show();
        Toast.makeText(context, "Must select an image to add.", Toast.LENGTH_LONG).show();


Java Source Code List