Home | History | Annotate | Download | only in shortcutlauncherdemo
      1 /*
      2  * Copyright (C) 2016 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 package com.example.android.pm.shortcutlauncherdemo;
     17 
     18 import android.app.Activity;
     19 import android.content.pm.LauncherApps;
     20 import android.content.pm.ShortcutInfo;
     21 import android.graphics.drawable.Drawable;
     22 import android.os.Bundle;
     23 import android.util.Log;
     24 import android.view.View;
     25 import android.view.View.OnClickListener;
     26 import android.widget.ImageView;
     27 import android.widget.TextView;
     28 
     29 public class PinShortcutConfirm extends Activity implements OnClickListener {
     30     private static final String TAG = "ShortcutConfirm";
     31 
     32     protected LauncherApps mLauncherApps;
     33 
     34     private LauncherApps.PinItemRequest mRequest;
     35     private ShortcutInfo mShortcutInfo;
     36 
     37     @Override
     38     protected void onCreate(Bundle savedInstanceState) {
     39         super.onCreate(savedInstanceState);
     40 
     41         setContentView(R.layout.pin_shortcut_confirm);
     42 
     43         mLauncherApps = getSystemService(LauncherApps.class);
     44 
     45         findViewById(R.id.ok).setOnClickListener(this);
     46         findViewById(R.id.cancel).setOnClickListener(this);
     47 
     48         mRequest = getSystemService(LauncherApps.class)
     49                 .getPinItemRequest(getIntent());
     50 
     51         mShortcutInfo = mRequest.getShortcutInfo();
     52 
     53         final TextView description = (TextView) findViewById(R.id.shortcut_description);
     54         description.setText("ID: " + mShortcutInfo.getId() + "\n"
     55                 + "Label: " + mShortcutInfo.getShortLabel());
     56 
     57         final Drawable icon = mLauncherApps.getShortcutIconDrawable(mShortcutInfo, 0);
     58         if (icon != null) {
     59             ((ImageView) findViewById(R.id.image)).setImageDrawable(icon);
     60         }
     61     }
     62 
     63     @Override
     64     public void onClick(View v) {
     65         switch (v.getId()) {
     66             case R.id.ok:
     67                 acceptShortcut();
     68                 finish();
     69                 break;
     70 
     71             case R.id.cancel:
     72                 finish();
     73                 break;
     74         }
     75     }
     76 
     77     private void acceptShortcut() {
     78         final boolean result = mRequest.accept();
     79         Log.i(TAG, "Accept returned: " + result);
     80         mRequest = null;
     81     }
     82 }
     83