Skip to content

Instantly share code, notes, and snippets.

@chris-carneiro
chris-carneiro / link-spring-dao-jpa.txt
Last active March 14, 2018 15:58
[LINKS] [Spring]: DAO : JPA
@chris-carneiro
chris-carneiro / AutoScrollHelper.java
Created January 17, 2018 16:45
Endless autoscrolling for viewPager
public static ObjectAnimator scrollDownSmoothlyIndefinitely(@NonNull WeakReference<NestedScrollView> scrollViewRef, int scrollingDuration) {
NestedScrollView scrollView = scrollViewRef.get();
if (scrollView != null) {
final ObjectAnimator animScrollDown = ObjectAnimator.ofInt(scrollView, "scrollY",
scrollView.getChildAt(0).getHeight());
animScrollDown.setInterpolator(new LinearInterpolator());
animScrollDown.setDuration(scrollingDuration);
@chris-carneiro
chris-carneiro / CorruptedTimeApproximationHelper.java
Last active January 5, 2018 10:56
API used to approximate a time [hh:mm] in seconds that was mistakenly multiplied by 24.
/**
* Approximates the minutes given a time in seconds that was mistakenly multiplied by 24.<BR/>
* Here is the wrong formula used in the first place. It was used to supposedly calculate a time [hh:mm] in seconds:<BR/>
* <pre>hourOfDay * 24 * 3600 + minute * 60</pre>
* Here's the formula we've come up with to approximate the minutes:<BR/>
* <pre>m ~= (x/60) - (1440*E(x/86400))</pre>
* Where x is the time in seconds and E(h) is the hours rounded to floor.
*
* @param seconds time [hh::mm] in seconds that was multiplied by 24.
* @return int
@chris-carneiro
chris-carneiro / httpd.conf
Last active August 9, 2017 09:15
Secured apache configuration
######## Security ########
## Silents ServerSignature response
ServerSignature Off
ServerTokens Prod
<IfModule mod_headers.c>
## Forces httpOnly and Secure Cookies - Mitigate XSS Attacks
Header edit Set-Cookie ^(.*)$ $1;HttpOnly;Secure
@chris-carneiro
chris-carneiro / .htaccess
Last active August 9, 2017 09:22
.htaccess that makes apache return "not found" for requests that either lead to a 404 or 403.
ErrorDocument 404 "Not found" Or http://yourdomain.com ## Redirects 404 Error to home page
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/(.*)$ - [R=404,L] ## Returns Error 404 when trying to access a subfolder
## Returns 404 when trying to access the current .htaccess file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.(htpasswd|htaccess|aahtpasswd).*\ HTTP/ [NC]
RewriteRule .? - [R=404,NS,L]
@chris-carneiro
chris-carneiro / WebSecurityConfigurer.java
Last active February 1, 2018 13:53
Example of how to protect actuator endpoints with a basic auth
import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint;
@chris-carneiro
chris-carneiro / proguard-rules.pro
Last active September 28, 2022 06:30
Proguard rules sample for Retrofit2, Gson, OrmLite, Javamail, Otto, disable logging
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /home/chris/Android/Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
@chris-carneiro
chris-carneiro / MailClient.java
Created June 29, 2017 13:34
Simple Mail client that uses JavaMail for android
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread;
@chris-carneiro
chris-carneiro / TokenManager.java
Created June 29, 2017 13:29
Simple token manager for android
public class TokenManager {
private static final String TAG = TokenManager.class.getName();
private static TokenManager instance;
private String token;
private TokenManager() {
token = "";
@chris-carneiro
chris-carneiro / APIKeyInterceptor.java
Created June 29, 2017 13:28
Simple class that adds a secret key to each ws request using okhttp3
public class APIKeyInterceptor implements Interceptor {
private static final String SECRET_KEY_HEADER = "secret_key";
private static final String UUID_HEADER = "uuid";
private static final String TAG = "APIKeyInterceptor";
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Log.d(TAG, "intercept ===============> "+request.url());