1

I develop an Android application and I need to create a "fake lockscreen". I explain me.

The application should display a black screen like if the screen was locked (easy), then I want to display a fake lockscreen when the user tap on the lock/unlock phone button.

My problem is, when I set the application background with the current wallpaper (get with wallpaper manager) the background is not "cropped". The background image fit on the screen instead of, like in the "home screen" of the phone, fit on the number of desktop.

I try to use getBuiltInDrawable(FLAG_LOCK) but it's available only from API 24 and, when I try, the result is not what I'm expecting =/

Is there someone that have an idea on the question here ?

Thank you in advance. Baptiste.

Baptiste
  • 13
  • 1
  • 5
  • you should post some code and refactor your answer to your specific problem, that appears to be how you can change the Image Scaling Mode. – rupps Mar 22 '17 at 21:06
  • @rupps Yes I could, but beside the question of how to change the image scaling mode, I would like also to know about good practice. It's better to fake a lock screen or to really make a lock screen ? If I fake a lock screen, how can I display top system-UI but not bottom system-UI etc... If possible I would like to discuss about my general problem, how to organize my application in order to fake a lockscreen :) – Baptiste Mar 22 '17 at 21:21
  • 1
    I don't think you can really make a real lock screen AND make arbitrary devices use it, the lockscreen app runs with system privileges and you'll only be able to substitute it by injecting it in a custom rom or playing around with exotic stuff requiring root access. You'd better fake it with a fancy app and pray the user don't realize. But don't do nasty things ! Why not code the next Pokemon Go instead? – rupps Mar 22 '17 at 21:28
  • Because it's not what I want haha :D In fact, if I expose what I want it's : I want a fake lock screen and, depending on what is the pin code used to unlocking the phone, I display a different activity. An example : I'm a student, my prof come behind me, I run the app, black screen, Prof : "Hey student what are you doing ?!", Student : "I look for help to your exercise !!", seems to unlock the phone, enter the pin code 3003 and an image of chrome is displayed, but if it's the pin code 3004 it's an image of google maps or anything else ! Nothing nasty ! :) – Baptiste Mar 22 '17 at 21:37

1 Answers1

0

Ok, so you need to

1) Create your activity using a FullScreen Theme with no action bar, so it doesn't look like an app. To do that, you'll create a style in styles.xml and apply it in your manifest.xml

Full Screen Theme for AppCompat

2) For the background image, use an ImageView. ImageViews have a scaleType property out of the box that allows you to select different modes: FIT, Center Crop, etc ...

https://developer.android.com/reference/android/widget/ImageView.ScaleType.html

3) Create the rest of the logic to enter the Pin. and show your different mock images. There's a nice component that really fits your "project", its called ViewFlipper, and basically lets you to put all images inside and select which one to show. Your root layout will look more or less like this:

<ViewFlipper 
   android:id="@+id/flipper"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <!-- page 0: the lock screen mock -->
   <FrameLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>

      <ImageView
          android:src="@drawable/mock_lock_screen"
          android:scaleType="centerCrop"
          android:layout_width="match_parent"
          android:layout_height="match_parent"/>

       <EditText
          android:id="@+id/pinCode"
          android:layout_width="80dp"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:hint="PIN CODE" />
   </FrameLayout>

   <!-- page 1: chrome screen mock-->
   <ImageView
      android:src="@drawable/mock_chrome"
      android:scaleType="centerCrop"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>

   <!-- page 2: wikipedia screen mock-->
   <ImageView
      android:src="@drawable/mock_wiki"
      android:scaleType="centerCrop"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>

    .
    .
</ViewFlipper>

Then in the activity, you can show the lock screen and any mock page:

ViewFlipper mFlipper;

@Override
public void onCreate(Bundle savedInstanceState) {
   .
   .
   mFlipper = (ViewFlipper)findViewById(R.id.flipper);
  .
  .
  .
}

and to change the displayed screen

  mFlipper.setDisplayedChild(0); // will show lock screen
  mFlipper.setDisplayedChild(1); // will show chrome mock
  mFlipper.setDisplayedChild(2); // will show wikipedia mock

hope you get the idea! happy coding.

Community
  • 1
  • 1
rupps
  • 9,712
  • 4
  • 55
  • 95
  • Thank you so much ! I think it's what I am expecting for and all info I need are possibly here ! – Baptiste Mar 22 '17 at 23:30
  • So I try your solution, It's pretty cool and neaar what I want. Only remaining problem is that the background (same as wallpaper) don't fit correctly. Indeed, with a match_parent width, the width of the background is 1 screen. But if I am on the home screen of Android with, for example, 2 home screen, the width is 2 screen. Is there any class or service to get the number of home screen ? I found http://stackoverflow.com/questions/6086040/get-number-of-home-screens-in-android or DisplayMetrics but I'm not sure it's usefull in my case.. :( – Baptiste Mar 23 '17 at 22:49