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 REVEAL_THE_NAME = false;
57    public static final boolean FINISH = false;
58
59    FrameLayout mLayout;
60    int mTapCount;
61    int mKeyCount;
62    PathInterpolator mInterpolator = new PathInterpolator(0f, 0f, 0.5f, 1f);
63
64    @Override
65    protected void onCreate(Bundle savedInstanceState) {
66        super.onCreate(savedInstanceState);
67
68        mLayout = new FrameLayout(this);
69        setContentView(mLayout);
70    }
71
72    @Override
73    public void onAttachedToWindow() {
74        final DisplayMetrics dm = getResources().getDisplayMetrics();
75        final float dp = dm.density;
76        final int size = (int)
77                (Math.min(Math.min(dm.widthPixels, dm.heightPixels), 600*dp) - 100*dp);
78
79        final ImageView im = new ImageView(this);
80        final int pad = (int)(40*dp);
81        im.setPadding(pad, pad, pad, pad);
82        im.setTranslationZ(20);
83        im.setScaleX(0.5f);
84        im.setScaleY(0.5f);
85        im.setAlpha(0f);
86
87        im.setBackground(new RippleDrawable(
88                ColorStateList.valueOf(0xFFFFFFFF),
89                getDrawable(com.android.internal.R.drawable.platlogo),
90                null));
91//        im.setOutlineProvider(new ViewOutlineProvider() {
92//            @Override
93//            public void getOutline(View view, Outline outline) {
94//                outline.setOval(0, 0, view.getWidth(), view.getHeight());
95//            }
96//        });
97        im.setClickable(true);
98        im.setOnClickListener(new View.OnClickListener() {
99            @Override
100            public void onClick(View v) {
101                im.setOnLongClickListener(new View.OnLongClickListener() {
102                    @Override
103                    public boolean onLongClick(View v) {
104                        if (mTapCount < 5) return false;
105
106                        if (REVEAL_THE_NAME) {
107                            final Drawable overlay = getDrawable(
108                                com.android.internal.R.drawable.platlogo_m);
109                            overlay.setBounds(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
110                            im.getOverlay().clear();
111                            im.getOverlay().add(overlay);
112                            overlay.setAlpha(0);
113                            ObjectAnimator.ofInt(overlay, "alpha", 0, 255)
114                                .setDuration(500)
115                                .start();
116                        }
117
118                        final ContentResolver cr = getContentResolver();
119                        if (Settings.System.getLong(cr, Settings.System.EGG_MODE, 0)
120                                == 0) {
121                            // For posterity: the moment this user unlocked the easter egg
122                            try {
123                                Settings.System.putLong(cr,
124                                        Settings.System.EGG_MODE,
125                                        System.currentTimeMillis());
126                            } catch (RuntimeException e) {
127                                Log.e("PlatLogoActivity", "Can't write settings", e);
128                            }
129                        }
130                        im.post(new Runnable() {
131                            @Override
132                            public void run() {
133                                try {
134                                    startActivity(new Intent(Intent.ACTION_MAIN)
135                                            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
136                                                    | Intent.FLAG_ACTIVITY_CLEAR_TASK
137                                                    | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
138                                            .addCategory("com.android.internal.category.PLATLOGO"));
139                                } catch (ActivityNotFoundException ex) {
140                                    Log.e("PlatLogoActivity", "No more eggs.");
141                                }
142                                if (FINISH) finish();
143                            }
144                        });
145                        return true;
146                    }
147                });
148                mTapCount++;
149            }
150        });
151
152        // Enable hardware keyboard input for TV compatibility.
153        im.setFocusable(true);
154        im.requestFocus();
155        im.setOnKeyListener(new View.OnKeyListener() {
156            @Override
157            public boolean onKey(View v, int keyCode, KeyEvent event) {
158                if (keyCode != KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
159                    ++mKeyCount;
160                    if (mKeyCount > 2) {
161                        if (mTapCount > 5) {
162                            im.performLongClick();
163                        } else {
164                            im.performClick();
165                        }
166                    }
167                    return true;
168                } else {
169                    return false;
170                }
171            }
172        });
173
174        mLayout.addView(im, new FrameLayout.LayoutParams(size, size, Gravity.CENTER));
175
176        im.animate().scaleX(1f).scaleY(1f).alpha(1f)
177                .setInterpolator(mInterpolator)
178                .setDuration(500)
179                .setStartDelay(800)
180                .start();
181    }
182}
183