1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.app;
18
19import android.animation.Animator;
20import android.animation.ObjectAnimator;
21import android.annotation.Nullable;
22import android.app.Activity;
23import android.content.ActivityNotFoundException;
24import android.content.ContentResolver;
25import android.content.Intent;
26import android.content.res.ColorStateList;
27import android.graphics.Canvas;
28import android.graphics.Color;
29import android.graphics.ColorFilter;
30import android.graphics.Outline;
31import android.graphics.Paint;
32import android.graphics.Path;
33import android.graphics.PixelFormat;
34import android.graphics.PorterDuff;
35import android.graphics.PorterDuffColorFilter;
36import android.graphics.drawable.Drawable;
37import android.graphics.drawable.GradientDrawable;
38import android.graphics.drawable.RippleDrawable;
39import android.graphics.drawable.ShapeDrawable;
40import android.graphics.drawable.shapes.OvalShape;
41import android.os.Bundle;
42import android.provider.Settings;
43import android.util.DisplayMetrics;
44import android.util.Log;
45import android.util.MathUtils;
46import android.view.Gravity;
47import android.view.KeyEvent;
48import android.view.View;
49import android.view.ViewGroup;
50import android.view.ViewOutlineProvider;
51import android.view.animation.PathInterpolator;
52import android.widget.FrameLayout;
53import android.widget.ImageView;
54
55public class PlatLogoActivity extends Activity {
56    public static final boolean FINISH = true;
57
58    FrameLayout mLayout;
59    int mTapCount;
60    int mKeyCount;
61    PathInterpolator mInterpolator = new PathInterpolator(0f, 0f, 0.5f, 1f);
62
63    @Override
64    protected void onCreate(Bundle savedInstanceState) {
65        super.onCreate(savedInstanceState);
66
67        mLayout = new FrameLayout(this);
68        setContentView(mLayout);
69    }
70
71    @Override
72    public void onAttachedToWindow() {
73        final DisplayMetrics dm = getResources().getDisplayMetrics();
74        final float dp = dm.density;
75        final int size = (int)
76                (Math.min(Math.min(dm.widthPixels, dm.heightPixels), 600*dp) - 100*dp);
77
78        final ImageView im = new ImageView(this);
79        final int pad = (int)(40*dp);
80        im.setPadding(pad, pad, pad, pad);
81        im.setTranslationZ(20);
82        im.setScaleX(0.5f);
83        im.setScaleY(0.5f);
84        im.setAlpha(0f);
85
86        im.setBackground(new RippleDrawable(
87                ColorStateList.valueOf(0xFF776677),
88                getDrawable(com.android.internal.R.drawable.platlogo),
89                null));
90        im.setOutlineProvider(new ViewOutlineProvider() {
91            @Override
92            public void getOutline(View view, Outline outline) {
93                final int w = view.getWidth();
94                final int h = view.getHeight();
95                outline.setOval((int)(w*.125), (int)(h*.125), (int)(w*.96), (int)(h*.96));
96            }
97        });
98        im.setElevation(12f*dp);
99        im.setClickable(true);
100        im.setOnClickListener(new View.OnClickListener() {
101            @Override
102            public void onClick(View v) {
103                im.setOnLongClickListener(new View.OnLongClickListener() {
104                    @Override
105                    public boolean onLongClick(View v) {
106                        if (mTapCount < 5) return false;
107
108                        final ContentResolver cr = getContentResolver();
109                        if (Settings.System.getLong(cr, Settings.System.EGG_MODE, 0)
110                                == 0) {
111                            // For posterity: the moment this user unlocked the easter egg
112                            try {
113                                Settings.System.putLong(cr,
114                                        Settings.System.EGG_MODE,
115                                        System.currentTimeMillis());
116                            } catch (RuntimeException e) {
117                                Log.e("PlatLogoActivity", "Can't write settings", e);
118                            }
119                        }
120                        im.post(new Runnable() {
121                            @Override
122                            public void run() {
123                                try {
124                                    startActivity(new Intent(Intent.ACTION_MAIN)
125                                            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
126                                                    | Intent.FLAG_ACTIVITY_CLEAR_TASK
127                                                    | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
128                                            .addCategory("com.android.internal.category.PLATLOGO"));
129                                } catch (ActivityNotFoundException ex) {
130                                    Log.e("PlatLogoActivity", "No more eggs.");
131                                }
132                                if (FINISH) finish();
133                            }
134                        });
135                        return true;
136                    }
137                });
138                mTapCount++;
139            }
140        });
141
142        // Enable hardware keyboard input for TV compatibility.
143        im.setFocusable(true);
144        im.requestFocus();
145        im.setOnKeyListener(new View.OnKeyListener() {
146            @Override
147            public boolean onKey(View v, int keyCode, KeyEvent event) {
148                if (keyCode != KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
149                    ++mKeyCount;
150                    if (mKeyCount > 2) {
151                        if (mTapCount > 5) {
152                            im.performLongClick();
153                        } else {
154                            im.performClick();
155                        }
156                    }
157                    return true;
158                } else {
159                    return false;
160                }
161            }
162        });
163
164        mLayout.addView(im, new FrameLayout.LayoutParams(size, size, Gravity.CENTER));
165
166        im.animate().scaleX(1f).scaleY(1f).alpha(1f)
167                .setInterpolator(mInterpolator)
168                .setDuration(500)
169                .setStartDelay(800)
170                .start();
171    }
172}
173