Home | History | Annotate | Download | only in app
      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 
     17 package com.android.internal.app;
     18 
     19 import android.animation.ObjectAnimator;
     20 import android.app.Activity;
     21 import android.content.ActivityNotFoundException;
     22 import android.content.ContentResolver;
     23 import android.content.Intent;
     24 import android.content.res.ColorStateList;
     25 import android.graphics.Canvas;
     26 import android.graphics.Outline;
     27 import android.graphics.Paint;
     28 import android.graphics.Path;
     29 import android.graphics.drawable.Drawable;
     30 import android.graphics.drawable.GradientDrawable;
     31 import android.graphics.drawable.RippleDrawable;
     32 import android.graphics.drawable.ShapeDrawable;
     33 import android.graphics.drawable.shapes.OvalShape;
     34 import android.os.Bundle;
     35 import android.provider.Settings;
     36 import android.util.DisplayMetrics;
     37 import android.util.Log;
     38 import android.view.Gravity;
     39 import android.view.KeyEvent;
     40 import android.view.View;
     41 import android.view.ViewGroup;
     42 import android.view.ViewOutlineProvider;
     43 import android.view.animation.PathInterpolator;
     44 import android.widget.FrameLayout;
     45 import android.widget.ImageView;
     46 
     47 public class PlatLogoActivity extends Activity {
     48     final static int[] FLAVORS = {
     49             0xFF9C27B0, 0xFFBA68C8, // grape
     50             0xFFFF9800, 0xFFFFB74D, // orange
     51             0xFFF06292, 0xFFF8BBD0, // bubblegum
     52             0xFFAFB42B, 0xFFCDDC39, // lime
     53             0xFFFFEB3B, 0xFFFFF176, // lemon
     54             0xFF795548, 0xFFA1887F, // mystery flavor
     55     };
     56     FrameLayout mLayout;
     57     int mTapCount;
     58     int mKeyCount;
     59     PathInterpolator mInterpolator = new PathInterpolator(0f, 0f, 0.5f, 1f);
     60 
     61     static int newColorIndex() {
     62         return 2*((int) (Math.random()*FLAVORS.length/2));
     63     }
     64 
     65     Drawable makeRipple() {
     66         final int idx = newColorIndex();
     67         final ShapeDrawable popbg = new ShapeDrawable(new OvalShape());
     68         popbg.getPaint().setColor(FLAVORS[idx]);
     69         final RippleDrawable ripple = new RippleDrawable(
     70                 ColorStateList.valueOf(FLAVORS[idx+1]),
     71                 popbg, null);
     72         return ripple;
     73     }
     74 
     75     @Override
     76     protected void onCreate(Bundle savedInstanceState) {
     77         super.onCreate(savedInstanceState);
     78 
     79         mLayout = new FrameLayout(this);
     80         setContentView(mLayout);
     81     }
     82 
     83     @Override
     84     public void onAttachedToWindow() {
     85         final DisplayMetrics dm = getResources().getDisplayMetrics();
     86         final float dp = dm.density;
     87         final int size = (int)
     88                 (Math.min(Math.min(dm.widthPixels, dm.heightPixels), 600*dp) - 100*dp);
     89 
     90         final View stick = new View(this) {
     91             Paint mPaint = new Paint();
     92             Path mShadow = new Path();
     93 
     94             @Override
     95             public void onAttachedToWindow() {
     96                 super.onAttachedToWindow();
     97                 setWillNotDraw(false);
     98                 setOutlineProvider(new ViewOutlineProvider() {
     99                     @Override
    100                     public void getOutline(View view, Outline outline) {
    101                         outline.setRect(0, getHeight() / 2, getWidth(), getHeight());
    102                     }
    103                 });
    104             }
    105             @Override
    106             public void onDraw(Canvas c) {
    107                 final int w = c.getWidth();
    108                 final int h = c.getHeight() / 2;
    109                 c.translate(0, h);
    110                 final GradientDrawable g = new GradientDrawable();
    111                 g.setOrientation(GradientDrawable.Orientation.LEFT_RIGHT);
    112                 g.setGradientCenter(w * 0.75f, 0);
    113                 g.setColors(new int[] { 0xFFFFFFFF, 0xFFAAAAAA });
    114                 g.setBounds(0, 0, w, h);
    115                 g.draw(c);
    116                 mPaint.setColor(0xFFAAAAAA);
    117                 mShadow.reset();
    118                 mShadow.moveTo(0,0);
    119                 mShadow.lineTo(w, 0);
    120                 mShadow.lineTo(w, size/2 + 1.5f*w);
    121                 mShadow.lineTo(0, size/2);
    122                 mShadow.close();
    123                 c.drawPath(mShadow, mPaint);
    124             }
    125         };
    126         mLayout.addView(stick, new FrameLayout.LayoutParams((int) (32 * dp),
    127                 ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER_HORIZONTAL));
    128         stick.setAlpha(0f);
    129 
    130         final ImageView im = new ImageView(this);
    131         im.setTranslationZ(20);
    132         im.setScaleX(0);
    133         im.setScaleY(0);
    134         final Drawable platlogo = getDrawable(com.android.internal.R.drawable.platlogo);
    135         platlogo.setAlpha(0);
    136         im.setImageDrawable(platlogo);
    137         im.setBackground(makeRipple());
    138         im.setClickable(true);
    139         final ShapeDrawable highlight = new ShapeDrawable(new OvalShape());
    140         highlight.getPaint().setColor(0x10FFFFFF);
    141         highlight.setBounds((int)(size*.15f), (int)(size*.15f),
    142                             (int)(size*.6f), (int)(size*.6f));
    143         im.getOverlay().add(highlight);
    144         im.setOnClickListener(new View.OnClickListener() {
    145             @Override
    146             public void onClick(View v) {
    147                 if (mTapCount == 0) {
    148                     im.animate()
    149                             .translationZ(40)
    150                             .scaleX(1)
    151                             .scaleY(1)
    152                             .setInterpolator(mInterpolator)
    153                             .setDuration(700)
    154                             .setStartDelay(500)
    155                             .start();
    156 
    157                     final ObjectAnimator a = ObjectAnimator.ofInt(platlogo, "alpha", 0, 255);
    158                     a.setInterpolator(mInterpolator);
    159                     a.setStartDelay(1000);
    160                     a.start();
    161 
    162                     stick.animate()
    163                             .translationZ(20)
    164                             .alpha(1)
    165                             .setInterpolator(mInterpolator)
    166                             .setDuration(700)
    167                             .setStartDelay(750)
    168                             .start();
    169 
    170                     im.setOnLongClickListener(new View.OnLongClickListener() {
    171                         @Override
    172                         public boolean onLongClick(View v) {
    173                             if (mTapCount < 5) return false;
    174 
    175                             final ContentResolver cr = getContentResolver();
    176                             if (Settings.System.getLong(cr, Settings.System.EGG_MODE, 0)
    177                                     == 0) {
    178                                 // For posterity: the moment this user unlocked the easter egg
    179                                 Settings.System.putLong(cr,
    180                                         Settings.System.EGG_MODE,
    181                                         System.currentTimeMillis());
    182                             }
    183                             im.post(new Runnable() {
    184                                 @Override
    185                                 public void run() {
    186                                     try {
    187                                         startActivity(new Intent(Intent.ACTION_MAIN)
    188                                                 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
    189                                                         | Intent.FLAG_ACTIVITY_CLEAR_TASK
    190                                                         | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
    191                                                 .addCategory("com.android.internal.category.PLATLOGO"));
    192                                     } catch (ActivityNotFoundException ex) {
    193                                         Log.e("PlatLogoActivity", "No more eggs.");
    194                                     }
    195                                     finish();
    196                                 }
    197                             });
    198                             return true;
    199                         }
    200                     });
    201                 } else {
    202                     im.setBackground(makeRipple());
    203                 }
    204                 mTapCount++;
    205             }
    206         });
    207 
    208         // Enable hardware keyboard input for TV compatibility.
    209         im.setFocusable(true);
    210         im.requestFocus();
    211         im.setOnKeyListener(new View.OnKeyListener() {
    212             @Override
    213             public boolean onKey(View v, int keyCode, KeyEvent event) {
    214                 if (keyCode != KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
    215                     ++mKeyCount;
    216                     if (mKeyCount > 2) {
    217                         if (mTapCount > 5) {
    218                             im.performLongClick();
    219                         } else {
    220                             im.performClick();
    221                         }
    222                     }
    223                     return true;
    224                 } else {
    225                     return false;
    226                 }
    227             }
    228         });
    229 
    230         mLayout.addView(im, new FrameLayout.LayoutParams(size, size, Gravity.CENTER));
    231 
    232         im.animate().scaleX(0.3f).scaleY(0.3f)
    233                 .setInterpolator(mInterpolator)
    234                 .setDuration(500)
    235                 .setStartDelay(800)
    236                 .start();
    237     }
    238 }
    239