Skip to content

Instantly share code, notes, and snippets.

@granoeste
granoeste / MainActivity.java
Created August 29, 2012 08:13
[Android] Service Bind/Unbind
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
private final MainActivity self = this;
MainService mService;
MyServiceConnection mConn;
boolean mIsBound;
@Override
public void onCreate(Bundle savedInstanceState) {
@granoeste
granoeste / EachDirectoryPath.md
Last active April 4, 2024 22:32
[Android] How to get the each directory path.

System directories

Method Result
Environment.getDataDirectory() /data
Environment.getDownloadCacheDirectory() /cache
Environment.getRootDirectory() /system

External storage directories

@granoeste
granoeste / _main.java
Created May 2, 2012 05:48
[Android] Get the color from the theme.
// ---------------------------------------------------
// Get the color and more attributes from the theme.
// ---------------------------------------------------
TypedArray ta = context.getTheme().obtainStyledAttributes(R.styleable.ViewStyle);
int labelColor = ta.getColor(R.styleable.ViewStyle_labelColor, defValue);
int dividerColor = ta.getColor(R.styleable.ViewStyle_dividerColor, defValue);
float textSize = ta.getDimension(R.styleable.ViewStyle_textSize, defValue);
@granoeste
granoeste / activity_main.xml
Created August 13, 2012 07:34
[Android] Glossy gradient button
<Button
android:id="@+id/button1"
style="@style/GlossyGradientButtonStyle"
android:layout_width="wrap_content"
android:layout_height="@dimen/button_height"
android:minWidth="100dp"
android:text="OK" />
<Button
android:id="@+id/button2"
@granoeste
granoeste / date_time_serializer.dart
Last active February 16, 2022 08:43
RFC3339 DateTime Serializer
class DateTimeSerializer {
static DateTime? deserialize(String? serialized) {
if (serialized == null) return null;
final serializedString = serialized;
final fixed = serializedString.replaceAllMapped(
RegExp(r'(.*:\d\d)(\.\d+)?(Z|[+-]\d{2}:\d{2})'),
(match) {
// #1 capturing group is date and time without a fraction of a second
@granoeste
granoeste / Resource.kt
Created January 13, 2022 02:25
A generic class that holds a value with its loading status.
/**
* A generic class that holds a value with its loading status.
* @param <T>
*/
// ImmutableClass
class Resource<T> private constructor(
val status: Status,
val data: T? = null,
val throwable: Throwable? = null,
val id: Long = 0
@granoeste
granoeste / webkitmediasource-is-type-supported.html
Last active December 26, 2021 10:54
[JavaScript][HTML5][MediaSource] MediaSource.isTypeSupported
<!DOCTYPE html>
<html>
<head>
<script>
window.MediaSource = window.MediaSource || window.WebKitMediaSource;
function testTypes(types) {
for (var i = 0; i < types.length; ++i)
console.log("MediaSource.isTypeSupported(" + types[i] + ") : " + MediaSource.isTypeSupported(types[i]));
}
@granoeste
granoeste / pretty_log_printer.dart
Last active December 17, 2021 12:15
Pretty Log Printer
import 'dart:convert';
import 'package:logger/logger.dart';
class PrettyLogPrinter extends LogPrinter {
static final levelColors = {
Level.verbose: AnsiColor.fg(AnsiColor.grey(0.5)),
Level.debug: AnsiColor.none(),
Level.info: AnsiColor.fg(12),
Level.warning: AnsiColor.fg(208),
@granoeste
granoeste / gist:858096
Created March 7, 2011 05:01
[Android] Phone State Listener
public class SignalStrengthListener extends PhoneStateListener {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
boolean isGsm = signalStrength.isGsm();
// Get the CDMA RSSI value in dBm
int iCdmaDbm = signalStrength.getCdmaDbm();
// Get the CDMA Ec/Io value in dB*10
@granoeste
granoeste / Setup maven-android.md
Last active September 14, 2021 19:22
[Android] setup maven-android