Giter Site home page Giter Site logo

androidquickutils's Introduction

QuickUtils

This repository offers a set of random useful classes to deal with repetitive tasks in the Android Framework. Intended to help you getting your Android applications off the ground quickly, by offering ready-to-use components and utility classes that wrap a lot of the boilerplate that's involved when writing Android apps.

Changelog

0.3 2012/08/26

  • Added Javadocs to the project
  • All the log methods now can handle Throwable objects, thanks @joelfernandes for the suggestion
  • Added copyFile(fromFile, toFile) to the sdcard category
  • Added copyFile(fromFileInputStream, toFileOutputStream) to the sdcard category
  • Added getSDCardPath to the sdcardcategory
  • Added web category
  • hasInternetConnection() moved from misc to web category
  • Added HTTPGetRequest() to the webcategory
  • Added degreesToRadians, radiansTdoDegrees, acos, asin, atan, atan2, tan, max, min, abs, logarithm, exponencial and isEven to the math category
  • getCurrentTimeInSeconds() and getCurrentTimeInMilliseconds() moved to the date category
  • AddedgetDayAsString(int day, String format) and getDayAsDate(int day) to the date category

0.2 2012/08/02

  • Checks if the app has connectivity to the Internet
  • getCurrentTime() is now divided into getCurrentTimeInSeconds() and getCurrentTimeInMilliseconds()
  • public static long getCurrentTimeInSeconds()
  • sleep method now accepts milliseconds instead of seconds
  • log now has a warning method
  • Updated max SDK version to 4.0.3
  • Added math category
  • Get a random number between a given range
  • Check if a number is odd

0.1 2012/07/10

  • Added log category
  • Added error log method
  • Added information log method
  • Added verbose log method
  • Added debug log method
  • Added sdcard category
  • Added isSDCardAvailable method
  • Added isSDCardWritable method
  • Added misc category
  • Added vibrate method
  • Added sleep method
  • Added toast method with custom lenght time
  • Added get current time in miliseconds method

Usage

Really simple usage, you just need to specify the category and the method you want to use.

QuickUtils.__category__.__method__

LOG category

With this methods you don't need to set the TAG variable in every class of your project and you can disable the logs everywhere without deleting/commenting the log lines by setting the debug mode to PRODUCTION (explained in the previous section).

QuickUtils.log.__method__

Error Log

QuickUtils.log.e("error description");
QuickUtils.log.e("error description", throwable);

Verbose Log

QuickUtils.log.v("verbose description");
QuickUtils.log.v("verbose description", throwable);

Information Log

QuickUtils.log.i("information description");
QuickUtils.log.i("information description", throwable);

Warning Log

QuickUtils.log.w("warning description");
QuickUtils.log.w("warning description", throwable);

Debug Log

QuickUtils.log.d("debug description");
QuickUtils.log.d("debug description", throwable);

DATE category

Date Utils

QuickUtils.date.__method__

Get the current time in milliseconds long

QuickUtils.date.getCurrentTimeInMiliseconds();

Get the current time in seconds long

QuickUtils.date.getCurrentTimeInSeconds();

Gets a date with a desired format as a String String

They "day" parameter can be provided as:

  • QuickUtils.date.YESTERDAY,
  • QuickUtils.date.TODAY or
  • QuickUtils.date.TOMORROW

The format can be provided as e.g. "yyyy-MM-dd HH:mm:ss"

QuickUtils.date.getDayAsString(int day, String format);

Gets the desired day as a Date Date

They "day" parameter can be provided as:

  • QuickUtils.date.YESTERDAY,
  • QuickUtils.date.TODAY or
  • QuickUtils.date.TOMORROW
QuickUtils.date.getDayAsDate(int day);

MISC category

Misc utils

QuickUtils.misc.__method__

Toast method with short duration void

QuickUtils.misc.toast(context, "This is a short toast");

Toast with non specified duration void

Either Toast.LENGTH_SHORT or Toast.LENGTH_LONG

QuickUtils.misc.toast(context, "This is a short toast", Toast.LENGTH_LONG);

Sleep void

Causes the thread which sent this message to sleep for the given interval of time (given in milliseconds). The precision is not guaranteed - the Thread may sleep more or less than requested.

QuickUtils.misc.sleep(durationInMilliseconds);

Start google activity of speechRecognition

needed on onActivityResult(int requestCode, int resultCode, Intent data) to call getSpeechRecognitionResults() to get the results)

QuickUtils.misc.speechRecognition(final Activity activity, int maxResults, String text);

Get all results from the Google Speech Recognition activity

To be called onActivityResult() Return an ArrayList with all results or null if was not possible to get any results

QuickUtils.misc.getSpeechRecognitionResults(int requestCode, int resultCode, Intent data);

Get the first result that matches the Result List from Google Speech Recognition activity and the Dictionary given

To be called onActivityResult() Return a String with the first result matched or null if was not possible to get any result

QuickUtils.misc.getSpeechRecognitionResultFromDicionary(int requestCode, int resultCode, Intent data, ArrayList<String> array);

Get first result from the Google Speech Recognition activity

To be called onActivityResult() Return a string containing the first result of what was recognized

QuickUtils.misc.getSpeechRecognitionFirstResult(int requestCode, int resultCode, Intent data);

MATH category

Math Utils.

QuickUtils.math.__method__

Returns a random number int

A random int between MIN inclusive and MAX exclusive.

QuickUtils.math.getRandomNumber(int min, int max);

Check if a number is Odd boolean

True if the num is odd and false if it's even

QuickUtils.math.isOdd(int num);

Check if a number is Even boolean

True if the num is even and false if it's odd

QuickUtils.math.isEven(int num);

Degrees to radians float

Returns the converted value

QuickUtils.math.degreesToRadians(float degrees);

Radians to degrees float

Returns the converted value

QuickUtils.math.radiansTdoDegrees(float radians);

Arc cosine float

Returns the closest double approximation of the arc cosine of the argument within the range [0..pi]. The returned result is within 1 ulp (unit in the last place) of the real result.

QuickUtils.math.acos(float value);

Arc sine float

Returns the closest double approximation of the arc sine of the argument within the range [-pi/2..pi/2]. The returned result is within 1 ulp (unit in the last place) of the real result.

QuickUtils.math.asin(float value);

Arc tangent float

Returns the closest double approximation of the arc tangent of the argument within the range [-pi/2..pi/2]. The returned result is within 1 ulp (unit in the last place) of the real result.

QuickUtils.math.atan(float value);

Arc tangent of y/x within the range [-pi..pi] float

Returns the closest double approximation of the arc tangent of y/x within the range [-pi..pi]. This is the angle of the polar representation of the rectangular coordinates (x,y). The returned result is within 2 ulps (units in the last place) of the real result.

QuickUtils.math.atan2(float value);

Tangent of an angle float

Returns the tangent

QuickUtils.math.tan(float angle);

Absolute value float

Returns the absolute value

QuickUtils.math.abs(float v);

Number's logarithm float

Returns the closest double approximation of the natural logarithm of the argument. The returned result is within 1 ulp (unit in the last place) of the real result.

QuickUtils.math.logarithm(float number);

Number's Exponencial float

Returns the closest double approximation of the natural logarithm of the argument. The returned result is within 1 ulp (unit in the last place) of the real result.

QuickUtils.math.exponencial(float number);

Gets the higher number float int

the higher number between a and b

QuickUtils.math.max(float a, float b);
QuickUtils.math.max(int a, int b);

Gets the lower number float int

the lower number between a and b

QuickUtils.math.min(float a, float b);
QuickUtils.math.min(int a, int b);

WEB category

Web Utils.

QuickUtils.web.__method__

Set wireless connectivity On

also this method will need the permissions "android.permission.CHANGE_WIFI_STATE" and "android.permission.ACCESS_WIFI_STATE" true if was set successfully and false if it wasn't

QuickUtils.web.changeWirelessState(Context context, boolean state);

Check if can connect to the server

also this method will need the permissions "android.permission.INTERNET" true if the connection returned a successful code

QuickUtils.web.checkServerConnection(URL u);

Check if can connect to the server

also this method will need the permissions "android.permission.INTERNET" true if the connection returned a successful code

QuickUtils.web.checkServerConnection(String serverURL);

QuickUtils.web.hasInternetConnection(Context context);

Does a GET request to a given url String

Note: Please use this method on an AsyncTask in order not to freeze the application unnecessarely (http://developer.android.com/guide/practices/responsiveness.html)

QuickUtils.web.HTTPGetRequest(String url);

SDCARD category

SDCard Utils.

QuickUtils.sdcard.__method__

Check if the SD Card is Available boolean

True if the sd card is available and false if it is not

QuickUtils.sdcard.isSDCardAvailable();

Check if the SD Card is Writable boolean

True if the sd card is writable and false if it is not

QuickUtils.sdcard.isSDCardWritable();

Get the path to the SDCard String

Return the complete path to the SDCard

QuickUtils.sdcard.getSDCardPath();

Check if file exists on SDCard or not

boolean - if file exist on SDCard or not

QuickUtils.sdcard.checkIfFileExists(String filePath)

Copy a file from a place to another void

Creates the specified toFile as a byte for byte copy of the fromFile. If toFile already exists, then it will be replaced with a copy of fromFile. The name and path oftoFile will be that of toFile.

Note: fromFile and toFile will be closed by this function.

As Files

QuickUtils.sdcard.copyFile(fromFile, toFile);

or with Input and Output Streams:

QuickUtils.sdcard.copyFile(fromFileInputStream, toFileOutputStream);

Example

@Override
 public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// SET ENVIRONMENT
		QuickUtils.setDebugMode(QuickUtils.DEVELOPER_MODE); // can be omited

		// SET THE DEBUG TAG
		QuickUtils.setTAG("SAMPLE_APP");

		QuickUtils.log.v(QuickUtils.math.logarithm(number) + "");

		try {
			QuickUtils.log.w("This is dangerous code");

			// do DANGEROUS STUFF
			// ...
			// ...

			QuickUtils.misc.toast(this, "The result of your dangerous calculations is: " + number);

		} catch (Exception exception) {
			QuickUtils.log.e("Exception thrown", exception);
		}

		// Check if you can write on your sdcard
		if (QuickUtils.sdcard.isSDCardAvailable() && QuickUtils.sdcard.isSDCardWritable()) {

			// you can write safely on your sdcard
		

			File path = new File(QuickUtils.sdcard.getSDCardPath() + "/appName/");

			File from = new File(path, "from.txt");
			File to = new File(path, "to.txt");

			try {
				QuickUtils.sdcard.copyFile(from, to);
				QuickUtils.log.i("Written to the sdcard");

			} catch (IOException e) {
				QuickUtils.log.e("IOException", e);
			}
		}

Downloads

All the versions can be found here

Instalation

As simple as going to your project's properties and include the QuickUtils.jar library or add the QuickUtils project as a library as shown below.

Setting up the environment

Set the default TAG for logcat debug purposes

QuickUtils.setTAG("DESIRED_TAG");

Debug mode

To enable the log outputs (This is the default behavior of the library so you don't need to set this up).

QuickUtils.setDebugMode(QuickUtils.DEVELOPER_MODE);

When the application is ready to go and you want to disable the log outputs.

QuickUtils.setDebugMode(QuickUtils.PRODUCTION_MODE);

AndroidManifest.xml

If you intend to use the vibration util don't forget to add the vibration permission, if you haven't already, in your <manifest>:

<uses-permission android:name="android.permission.VIBRATE" />   

If you intend to use the hasConnectivity method don't forget to add the network state access permission, if you haven't already, in your <manifest>:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

If you intend to use the HTTPGetRequest method, if you haven't already, in your <manifest>:

<uses-permission android:name="android.permission.INTERNET" />

Contributing

Want to contribute? Great!

  1. Fork it.
  2. Create a branch (git checkout -b my_branch)
  3. Commit your changes (git commit -am "Added changes")
  4. Push to the branch (git push origin my_branch)
  5. Create an Issue with a link to your branch
  6. Enjoy a refreshing Diet Coke and wait

License

Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0.html)

Authors

Contributors

androidquickutils's People

Contributors

cesarferreira avatar luispereira avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.