it.tidalwave.mobile.android.ui.AndroidUtilities Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* blueBill Mobile - open source birdwatching
* ==========================================
*
* Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
* http://bluebill.tidalwave.it/mobile/
*
***********************************************************************************************************************
*
* 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.
*
***********************************************************************************************************************
*
* $Id: AndroidUtilities.java,v 45ead75a8e5e 2010/07/13 21:53:26 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.mobile.android.ui;
import javax.annotation.Nonnull;
import java.util.Calendar;
import java.util.Date;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import org.openide.util.Lookup;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.util.logging.Logger;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.DatePicker;
import android.widget.TimePicker;
import java.io.BufferedInputStream;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id: $
*
**********************************************************************************************************************/
public class AndroidUtilities
{
private static final String CLASS = AndroidUtilities.class.getName();
private static final Logger logger = Logger.getLogger(CLASS);
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public static void setDate (final @Nonnull DatePickerDialog datePicker, final @Nonnull Date date)
{
final Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
datePicker.updateDate(calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static Date getDate (final @Nonnull DatePicker datePicker, final @Nonnull Date date)
{
final Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.YEAR, datePicker.getYear());
calendar.set(Calendar.MONTH, datePicker.getMonth());
calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
return calendar.getTime();
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public static void setDate (final @Nonnull TimePickerDialog timePicker, final @Nonnull Date date)
{
final Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
timePicker.updateTime(calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE));
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static Date getDate (final @Nonnull TimePicker timePicker, final @Nonnull Date date)
{
final Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static OnClickListener createShowSoftKeyboardOnClickListener (final @Nonnull View hostView)
{
return new OnClickListener()
{
public void onClick (final @Nonnull View view)
{
final Context context = Lookup.getDefault().lookup(Context.class);
final InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(hostView, 0);
}
};
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public static int visibility (final boolean visible)
{
return visible ? View.VISIBLE : View.INVISIBLE;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static Drawable loadImage (final @Nonnull String url)
throws NotFoundException
{
logger.info("loadImage(%s)", url);
InputStream is = null;
try
{
is = new BufferedInputStream((InputStream)(new URL(url).getContent()));
return Drawable.createFromStream(is, "src");
// return BitmapFactory.decodeStream(is);
}
catch (MalformedURLException e)
{
e.printStackTrace(); // FIXME
throw new NotFoundException(url.toString() + ": " + e);
}
catch (IOException e)
{
e.printStackTrace(); // FIXME
throw new NotFoundException(url.toString() + ": " + e);
}
finally
{
if (is != null)
{
try
{
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static Bitmap loadBitmap (final @Nonnull String url)
throws NotFoundException
{
logger.info("loadBitmap(%s)", url);
InputStream is = null;
try
{
is = new BufferedInputStream((InputStream)(new URL(url).getContent()));
return BitmapFactory.decodeStream(is);
}
catch (MalformedURLException e)
{
e.printStackTrace(); // FIXME
throw new NotFoundException(url.toString() + ": " + e);
}
catch (IOException e)
{
e.printStackTrace(); // FIXME
throw new NotFoundException(url.toString() + ": " + e);
}
finally
{
if (is != null)
{
try
{
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
}