package uz.efir.muazzin;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
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[] DESK_CLOCK_PACKAGES = new String[] {
            "com.google.android.deskclock", // Google's
            "com.android.deskclock", // AOSP's
    };
    private static final String[] OTHER_CLOCK_PACKAGES = new String[] {
            "com.sec.android.app.clockpackage" // Samsung's
    };
    private static final String SELECT_TAB_INTENT_EXTRA = "deskclock.select.tab";
    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 : DESK_CLOCK_PACKAGES) {
            try {
                PackageInfo pi = pm.getPackageInfo(packageName, 0);
                if (302 >= pi.versionCode) {
                    Intent intent = pm.getLaunchIntentForPackage(packageName);
                    intent.putExtra(SELECT_TAB_INTENT_EXTRA, 0);
                    return intent;
                }
                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) {
            }
        }

        // None worked, try others
        for (String packageName : OTHER_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);
//    }
}

@@ -157,172 +157,13 @@
 kage
-Info;%0Aimport android.content.pm.PackageManager;%0A%0Aimport islam.adhanalarm.widget.NextNotificationWidgetProvider;%0Aimport islam.adhanalarm.widget.TimetableWidgetProvid
+Manag
 er;%0A
@@ -974,33 +974,32 @@
 S = new String%5B%5D
- 
 %7B%0A            %22c
@@ -1163,17 +1163,16 @@
 String%5B%5D
- 
 %7B%0A      
@@ -1233,38 +1233,37 @@
     %7D;%0A    p
-rivate
+ublic
  static 
 final String
@@ -1254,162 +1254,39 @@
 tic 
-final String SELECT_TAB_INTENT_EXTRA = %22deskclock.select.tab%22;%0A    private static final String DEFAULT_ALARM_ACTIVITY = %22com.android.deskclock.AlarmClock%22
+boolean isRestartNeeded = false
 ;%0A  
@@ -1337,70 +1337,20 @@
 se;%0A
+%0A
     p
-ublic static boolean isRestartNeeded = false;%0A%0A    public
+rivate
  sta
@@ -1374,14 +1374,13 @@
 ault
-Alarms
+Clock
 Inte
@@ -1552,25 +1552,8 @@
     
- PackageInfo pi =
  pm.
@@ -1604,564 +1604,56 @@
     
-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                ComponentName cn = new ComponentName(packageName, DEFAULT_ALARM_ACTIVITY);%0A                pm.getActivityInfo(cn, 0);%0A                Intent intent = new Intent(Intent.ACTION_MAIN);%0A                intent.addCategory(Intent.CATEGORY_LAUNCHER);%0A                intent.setComponent(cn);%0A                return intent
+return pm.getLaunchIntentForPackage(packageName)
 ;%0A  
@@ -1735,33 +1735,32 @@
     %7D%0A        %7D%0A
-%0A
         // None 
@@ -2146,32 +2146,576 @@
 rn null;%0A    %7D%0A%0A
+    public static Intent getDefaultAlarmsIntent(Context context) %7B%0A        PackageManager pm = context.getPackageManager();%0A        for (String packageName : DESK_CLOCK_PACKAGES) %7B%0A            try %7B%0A                ComponentName cn = new ComponentName(packageName, %22com.android.deskclock.AlarmClock%22);%0A                pm.getActivityInfo(cn, 0);%0A                return Intent.makeMainActivity(cn);%0A            %7D catch (PackageManager.NameNotFoundException ignored) %7B%0A            %7D%0A        %7D%0A        return getDefaultClockIntent(context);%0A    %7D%0A%0A
     public stati

Update alarm clock intent resolution logic