Home | History | Annotate | Download | only in multiwindow
      1 /*
      2  * Copyright (C) 2015 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.example.android.multiwindow;
     18 
     19 import android.app.Activity;
     20 import android.content.Intent;
     21 import android.os.Bundle;
     22 import android.view.View;
     23 import android.widget.TextView;
     24 
     25 public class LaunchingAdjacentActivity extends Activity implements View.OnClickListener {
     26 
     27     private static final String INSTANCE_NUMBER_KEY = "instance_number";
     28 
     29     private static int mInstanceCount;
     30     private int mInstanceNumber;
     31 
     32     @Override
     33     protected void onCreate(Bundle savedInstanceState) {
     34         super.onCreate(savedInstanceState);
     35         setContentView(R.layout.launching_adjacent_layout);
     36         findViewById(R.id.launch_settings_adjacent).setOnClickListener(this);
     37         findViewById(R.id.launch_new_task_single).setOnClickListener(this);
     38         findViewById(R.id.launch_new_task_multiple).setOnClickListener(this);
     39         findViewById(R.id.launch_new_task_adjacent).setOnClickListener(this);
     40         if (savedInstanceState != null) {
     41             mInstanceNumber = savedInstanceState.getInt(INSTANCE_NUMBER_KEY);
     42         } else {
     43             mInstanceNumber = mInstanceCount++;
     44         }
     45         ((TextView) findViewById(R.id.instance_number))
     46                 .setText(getString(R.string.instance_number) + mInstanceNumber);
     47     }
     48 
     49     @Override
     50     public void onClick(View v) {
     51         switch (v.getId()) {
     52             case R.id.launch_settings_adjacent: {
     53                 Intent intent = new Intent("android.settings.SETTINGS");
     54                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
     55                         | Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT);
     56                 startActivity(intent);
     57             }
     58             break;
     59             case R.id.launch_new_task_single: {
     60                 Intent intent = newAdjacentActivityIntent();
     61                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     62                 startActivity(intent);
     63             }
     64             break;
     65             case R.id.launch_new_task_multiple: {
     66                 Intent intent = newAdjacentActivityIntent();
     67                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
     68                 startActivity(intent);
     69             }
     70             break;
     71             case R.id.launch_new_task_adjacent: {
     72                 Intent intent = newAdjacentActivityIntent();
     73                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK
     74                         | Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT);
     75                 startActivity(intent);
     76             }
     77             break;
     78         }
     79     }
     80 
     81     private Intent newAdjacentActivityIntent() {
     82         Intent intent = new Intent(this, LaunchingAdjacentActivity.class);
     83         intent.setAction(Intent.ACTION_MAIN);
     84         intent.addCategory(Intent.CATEGORY_LAUNCHER);
     85         return intent;
     86     }
     87 
     88     @Override
     89     public void onSaveInstanceState(Bundle savedInstanceState) {
     90         savedInstanceState.putInt(INSTANCE_NUMBER_KEY, mInstanceNumber);
     91         super.onSaveInstanceState(savedInstanceState);
     92     }
     93 }
     94