package uz.efir.muazzin;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;

import islam.adhanalarm.widget.NextNotificationWidgetProvider;
import islam.adhanalarm.widget.TimetableWidgetProvider;

/*
 * Following alarm clock intent implementation is adapted from
 *     <a href="https://code.google.com/p/dashclock/">DashClock</a>
 *
 * Copyright 2013 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Utils {
    private static final String[] CLOCK_PACKAGES = new String[] {
        "com.google.android.deskclock", // Google's
        "com.android.deskclock", // AOSP's
        "com.sec.android.app.clockpackage" // Samsung's
    };
    private static final String DEFAULT_ALARM_ACTIVITY = "com.android.deskclock.AlarmClock";
    private static boolean mIsForeground = false;
    public static boolean isRestartNeeded = false;

    public static Intent getDefaultAlarmsIntent(Context context) {
        PackageManager pm = context.getPackageManager();
        for (String packageName : CLOCK_PACKAGES) {
            try {
                ComponentName cn = new ComponentName(packageName, DEFAULT_ALARM_ACTIVITY);
                pm.getActivityInfo(cn, 0);
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                intent.setComponent(cn);
                return intent;
            } catch (PackageManager.NameNotFoundException ignored) {
            }
        }

        // DEFAULT_ALARM_ACTIVITY is not found, launch the main app
        for (String packageName : CLOCK_PACKAGES) {
            try {
                pm.getPackageInfo(packageName, 0);
                return pm.getLaunchIntentForPackage(packageName);
            } catch (PackageManager.NameNotFoundException ignored) {
            }
        }

        // TODO: Add an option for user to set a custom app?
        return null;
    }

    public static boolean getIsForeground() {
        return mIsForeground;
    }

    public static void setIsForeground(boolean isForeground) {
        mIsForeground = isForeground;
    }

    public static void updateWidgets(Context context) {
        TimetableWidgetProvider.setLatestTimetable(context);
        NextNotificationWidgetProvider.setNextTime(context);
    }
}

@@ -120,16 +120,55 @@
 Intent;%0A
+import android.content.pm.PackageInfo;%0A
 import a
@@ -1107,16 +1107,21 @@
 tring%5B%5D 
+DESK_
 CLOCK_PA
@@ -1148,24 +1148,28 @@
 %5D %7B%0A        
+   
+ 
 %22com.google.
@@ -1204,24 +1204,28 @@
 e's%0A        
+    
 %22com.android
@@ -1247,16 +1247,99 @@
  AOSP's%0A
+    %7D;%0A    private static final String%5B%5D OTHER_CLOCK_PACKAGES = new String%5B%5D %7B%0A    
         
@@ -1385,32 +1385,114 @@
 amsung's%0A    %7D;%0A
+    private static final String SELECT_TAB_INTENT_EXTRA = %22deskclock.select.tab%22;%0A
     private stat
@@ -1820,32 +1820,37 @@
 g packageName : 
+DESK_
 CLOCK_PACKAGES) 
@@ -1861,32 +1861,342 @@
           try %7B%0A
+                PackageInfo pi = pm.getPackageInfo(packageName, 0);%0A                if (302 %3E= pi.versionCode) %7B%0A                    Intent intent = pm.getLaunchIntentForPackage(packageName);%0A                    intent.putExtra(SELECT_TAB_INTENT_EXTRA, 0);%0A                    return intent;%0A                %7D%0A
                 
@@ -2620,64 +2620,31 @@
  // 
-DEFAULT_ALARM_ACTIVITY is not found, launch the main app
+None worked, try others
 %0A   
@@ -2674,16 +2674,22 @@
 eName : 
+OTHER_
 CLOCK_PA

Add intent extra for Deskclock