com.pyamsoft.dontsuckmp.main.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.pyamsoft.dontsuckmp.main.MainActivity.java

Source

/*
 * Copyright 2016 Peter Kenji Yamanaka
 *
 * 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.
 */

package com.pyamsoft.dontsuckmp.main;

import android.app.Activity;
import android.databinding.DataBindingUtil;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.ActionBar;
import com.pyamsoft.dontsuckmp.BuildConfig;
import com.pyamsoft.dontsuckmp.R;
import com.pyamsoft.dontsuckmp.databinding.ActivityMainBinding;
import com.pyamsoft.pydroid.ads.AdSource;
import com.pyamsoft.pydroid.ui.ads.OnlineAdSource;
import com.pyamsoft.pydroid.ui.sec.TamperActivity;
import com.pyamsoft.pydroid.util.AppUtil;

public class MainActivity extends TamperActivity {

    @NonNull
    private final Handler handler = new Handler(Looper.getMainLooper());
    ActivityMainBinding binding;

    public static void colorStatusbar(@NonNull Activity activity, @ColorInt int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            activity.getWindow().setStatusBarColor(color);
        }
    }

    public static void overlapToolbar(@NonNull Activity activity, boolean overlap) {
        if (activity instanceof MainActivity) {
            final MainActivity mainActivity = (MainActivity) activity;
            final ActionBar actionBar = mainActivity.getSupportActionBar();
            final float elevation = overlap ? AppUtil.convertToDP(activity, 4) : 0;
            ViewCompat.setElevation(mainActivity.binding.toolbar, elevation);
            if (actionBar != null) {
                final Drawable background = overlap
                        ? new ColorDrawable(ContextCompat.getColor(activity, R.color.orange500))
                        : null;
                final String title = overlap ? activity.getString(R.string.app_name) : null;
                actionBar.setBackgroundDrawable(background);
                actionBar.setTitle(title);
            }
        } else {
            throw new IllegalStateException("Activity is not MainActivity");
        }
    }

    @Override
    protected boolean shouldCheckVersion() {
        return false;
    }

    @Override
    protected int bindActivityToView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        return R.id.ad_view;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme(R.style.Theme_DontSuck);
        super.onCreate(savedInstanceState);
    }

    @NonNull
    @Override
    protected String getSafePackageName() {
        return "com.pyamsoft.dontsuckmp";
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
        binding.unbind();
    }

    @NonNull
    @Override
    public String provideApplicationName() {
        return getString(R.string.app_name);
    }

    @Override
    public int getApplicationIcon() {
        return R.mipmap.ic_launcher;
    }

    @Override
    public int getCurrentApplicationVersion() {
        return BuildConfig.VERSION_CODE;
    }

    @NonNull
    @Override
    protected String[] getChangeLogLines() {
        return new String[] {};
    }

    @NonNull
    @Override
    protected String getVersionName() {
        return BuildConfig.VERSION_NAME;
    }

    @Nullable
    @Override
    protected AdSource provideOnlineAdSource() {
        OnlineAdSource source = new OnlineAdSource(R.string.banner_main_ad_id);
        source.addTestAdIds("E1241303FDC266381AD6C9FF6FAD056B");
        return source;
    }
}