All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.mobile.android.ui.AndroidUtilities Maven / Gradle / Ivy

/***********************************************************************************************************************
 *
 * blueBill Mobile - Android - open source birding
 * Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * 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.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://bluebill.tidalwave.it/mobile
 * SCM: https://java.net/hg/bluebill-mobile~android-src
 *
 **********************************************************************************************************************/
package it.tidalwave.mobile.android.ui;

import javax.annotation.Nonnull;
import java.util.Calendar;
import java.util.Date;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.openide.util.Lookup;
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.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.DatePicker;
import android.widget.TimePicker;
import lombok.Cleanup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
public class AndroidUtilities
  {
    private static final Logger log = LoggerFactory.getLogger(AndroidUtilities.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 IOException
      {
        log.info("loadImage({})", url);

        final @Cleanup InputStream is = new BufferedInputStream((InputStream)(new URL(url).getContent()));
        return Drawable.createFromStream(is, "src");
//            return BitmapFactory.decodeStream(is);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public static Bitmap loadBitmap (final @Nonnull String url)
      throws IOException
      {
        log.info("loadBitmap({})", url);

        final @Cleanup InputStream is = new BufferedInputStream((InputStream)(new URL(url).getContent()));
        return BitmapFactory.decodeStream(is);
      }
    
    /*******************************************************************************************************************
     *
     * FIXME: find a shorter way.
     *
     ******************************************************************************************************************/
    public static void runOnUIThread (final @Nonnull Runnable runnable) 
      {
        new AsyncTask() 
          {
            @Override
            protected Void doInBackground (Void... params)
              {
                return null;
              }

            @Override
            protected void onPostExecute (Void result) 
              {
                runnable.run();
              }
          }.execute(); 
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy